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