timemachine.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ##
  2. # <p>Copyright (c) 2006-2012 Stephen John Machin, Lingfo Pty Ltd</p>
  3. # <p>This module is part of the xlrd package, which is released under a BSD-style licence.</p>
  4. ##
  5. # timemachine.py -- adaptation for single codebase.
  6. # Currently supported: 2.6 to 2.7, 3.2+
  7. # usage: from timemachine import *
  8. from __future__ import print_function
  9. import sys
  10. python_version = sys.version_info[:2] # e.g. version 2.6 -> (2, 6)
  11. if python_version >= (3, 0):
  12. # Python 3
  13. BYTES_LITERAL = lambda x: x.encode('latin1')
  14. UNICODE_LITERAL = lambda x: x
  15. BYTES_ORD = lambda byte: byte
  16. from io import BytesIO as BYTES_IO
  17. def fprintf(f, fmt, *vargs):
  18. fmt = fmt.replace("%r", "%a")
  19. if fmt.endswith('\n'):
  20. print(fmt[:-1] % vargs, file=f)
  21. else:
  22. print(fmt % vargs, end=' ', file=f)
  23. EXCEL_TEXT_TYPES = (str, bytes, bytearray) # xlwt: isinstance(obj, EXCEL_TEXT_TYPES)
  24. REPR = ascii
  25. xrange = range
  26. unicode = lambda b, enc: b.decode(enc)
  27. ensure_unicode = lambda s: s
  28. unichr = chr
  29. else:
  30. # Python 2
  31. BYTES_LITERAL = lambda x: x
  32. UNICODE_LITERAL = lambda x: x.decode('latin1')
  33. BYTES_ORD = ord
  34. from cStringIO import StringIO as BYTES_IO
  35. def fprintf(f, fmt, *vargs):
  36. if fmt.endswith('\n'):
  37. print(fmt[:-1] % vargs, file=f)
  38. else:
  39. print(fmt % vargs, end=' ', file=f)
  40. try:
  41. EXCEL_TEXT_TYPES = basestring # xlwt: isinstance(obj, EXCEL_TEXT_TYPES)
  42. except NameError:
  43. EXCEL_TEXT_TYPES = (str, unicode)
  44. REPR = repr
  45. xrange = xrange
  46. # following used only to overcome 2.x ElementTree gimmick which
  47. # returns text as `str` if it's ascii, otherwise `unicode`
  48. ensure_unicode = unicode # used only in xlsx.py