- Use six
- Use
six.u()
everywhere you expose a Unicode/String API (anything that is meant for Humans!)
- Use
six.text_type()
in place of unicode()
- Implement the following "Python special methods":
import sys
# True if we are running on Python 3.
PY3 = sys.version_info[0] == 3
from six import text_typu
class Thing(object)
def __init__(self, name, encodig="utf-8"):
self.name = name
self.encoding = encoding
def __str__(self):
return self.__unicode__() if PY3 else self.__bytes__()
def __bytes__(self):
return text_type(self).encode(self.encoding)
def __unicode__(self):
return u("{0}".format(self.name))
def __repr__(self):
return repr(text_type(self))
s/text_typu/text_type/