Skip to content

Commit

Permalink
Simplify to_unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagnoux committed Aug 1, 2018
1 parent 048ef49 commit f5800c9
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions openfisca_core/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
from builtins import str


# The following two variables and the is_unicode function are there to bridge string types across Python 2 & 3
# The following variable and the to_unicode function are there to bridge string types across Python 2 & 3
basestring_type = (bytes, str)


def to_unicode(string):
"""
:param string: a string that needs to be unicoded
:param encoding: a string that represent the encoding type
:return: a unicode string
if the string is a python 2 str type, returns a unicode version of the string.
"""
if not isinstance(string, basestring_type):
string = str(string)
if isinstance(string, str):
return string

# Next line only gets triggered if the code is run in python 2
return string.decode('utf-8')
if isinstance(string, bytes):
return string.decode('utf-8')
return str(string)


class Dummy(object):
Expand Down

0 comments on commit f5800c9

Please sign in to comment.