six.py 538 B

123456789101112131415161718192021222324252627282930
  1. """Poor man's version Six"""
  2. import sys
  3. PY_VERSION = sys.version_info[0]
  4. def to_unicode(s):
  5. """Convert str s to unicode
  6. :param str s: string
  7. :return: "unicode" version of s (unicode in py2, str in py3)
  8. """
  9. if PY_VERSION == 2:
  10. s = unicode(s)
  11. return s
  12. def to_bytes(s, encoding="utf8"):
  13. """Converts str s to bytes"""
  14. if PY_VERSION == 2:
  15. b = bytes(s)
  16. elif PY_VERSION == 3:
  17. b = bytes(s, encoding)
  18. else:
  19. raise ValueError("Is Python 4 out already?")
  20. return b