__init__.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # coding=utf-8
  2. """
  3. past: compatibility with Python 2 from Python 3
  4. ===============================================
  5. ``past`` is a package to aid with Python 2/3 compatibility. Whereas ``future``
  6. contains backports of Python 3 constructs to Python 2, ``past`` provides
  7. implementations of some Python 2 constructs in Python 3 and tools to import and
  8. run Python 2 code in Python 3. It is intended to be used sparingly, as a way of
  9. running old Python 2 code from Python 3 until the code is ported properly.
  10. Potential uses for libraries:
  11. - as a step in porting a Python 2 codebase to Python 3 (e.g. with the ``futurize`` script)
  12. - to provide Python 3 support for previously Python 2-only libraries with the
  13. same APIs as on Python 2 -- particularly with regard to 8-bit strings (the
  14. ``past.builtins.str`` type).
  15. - to aid in providing minimal-effort Python 3 support for applications using
  16. libraries that do not yet wish to upgrade their code properly to Python 3, or
  17. wish to upgrade it gradually to Python 3 style.
  18. Here are some code examples that run identically on Python 3 and 2::
  19. >>> from past.builtins import str as oldstr
  20. >>> philosopher = oldstr(u'\u5b54\u5b50'.encode('utf-8'))
  21. >>> # This now behaves like a Py2 byte-string on both Py2 and Py3.
  22. >>> # For example, indexing returns a Python 2-like string object, not
  23. >>> # an integer:
  24. >>> philosopher[0]
  25. '\xe5'
  26. >>> type(philosopher[0])
  27. <past.builtins.oldstr>
  28. >>> # List-producing versions of range, reduce, map, filter
  29. >>> from past.builtins import range, reduce
  30. >>> range(10)
  31. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  32. >>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
  33. 15
  34. >>> # Other functions removed in Python 3 are resurrected ...
  35. >>> from past.builtins import execfile
  36. >>> execfile('myfile.py')
  37. >>> from past.builtins import raw_input
  38. >>> name = raw_input('What is your name? ')
  39. What is your name? [cursor]
  40. >>> from past.builtins import reload
  41. >>> reload(mymodule) # equivalent to imp.reload(mymodule) in Python 3
  42. >>> from past.builtins import xrange
  43. >>> for i in xrange(10):
  44. ... pass
  45. It also provides import hooks so you can import and use Python 2 modules like
  46. this::
  47. $ python3
  48. >>> from past.translation import autotranslate
  49. >>> authotranslate('mypy2module')
  50. >>> import mypy2module
  51. until the authors of the Python 2 modules have upgraded their code. Then, for
  52. example::
  53. >>> mypy2module.func_taking_py2_string(oldstr(b'abcd'))
  54. Credits
  55. -------
  56. :Author: Ed Schofield, Jordan M. Adler, et al
  57. :Sponsor: Python Charmers Pty Ltd, Australia: http://pythoncharmers.com
  58. Licensing
  59. ---------
  60. Copyright 2013-2019 Python Charmers Pty Ltd, Australia.
  61. The software is distributed under an MIT licence. See LICENSE.txt.
  62. """
  63. from future import __version__, __copyright__, __license__
  64. __title__ = 'past'
  65. __author__ = 'Ed Schofield'