Skip to content

Instantly share code, notes, and snippets.

@leafsummer
Created December 6, 2018 07:50
Show Gist options
  • Save leafsummer/2d875286fb11cd5c77fa0d3df6ea622a to your computer and use it in GitHub Desktop.
Save leafsummer/2d875286fb11cd5c77fa0d3df6ea622a to your computer and use it in GitHub Desktop.
[change text to unicode, compat python2 or python3]
import sys
PY2 = sys.version_info[0] == 2
if not PY2:
# Python 3.x and up
text_type = str
string_types = (str,)
def as_text(v):
if v is None:
return None
elif isinstance(v, bytes):
return v.decode('utf-8')
elif isinstance(v, str):
return v
else:
raise ValueError('Unknown type %r' % type(v))
else:
# Python 2.x
def text_type(v):
try:
return unicode(v) # noqa
except Exception:
return unicode(v, "utf-8", errors="ignore") # noqa
string_types = (str, unicode) # noqa
def as_text(v):
if v is None:
return None
elif isinstance(v, str):
return v.decode('utf-8')
elif isinstance(v, unicode): # noqa
return v
else:
raise Exception("Input cannot be decoded into literal thing.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment