Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/past/__init__.py: 80%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

5 statements  

1# coding=utf-8 

2""" 

3past: compatibility with Python 2 from Python 3 

4=============================================== 

5 

6``past`` is a package to aid with Python 2/3 compatibility. Whereas ``future`` 

7contains backports of Python 3 constructs to Python 2, ``past`` provides 

8implementations of some Python 2 constructs in Python 3 and tools to import and 

9run Python 2 code in Python 3. It is intended to be used sparingly, as a way of 

10running old Python 2 code from Python 3 until the code is ported properly. 

11 

12Potential 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 

23Here 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 

59It also provides import hooks so you can import and use Python 2 modules like 

60this:: 

61 

62 $ python3 

63 

64 >>> from past.translation import autotranslate 

65 >>> authotranslate('mypy2module') 

66 >>> import mypy2module 

67 

68until the authors of the Python 2 modules have upgraded their code. Then, for 

69example:: 

70 

71 >>> mypy2module.func_taking_py2_string(oldstr(b'abcd')) 

72 

73 

74Credits 

75------- 

76 

77:Author: Ed Schofield, Jordan M. Adler, et al 

78:Sponsor: Python Charmers: https://pythoncharmers.com 

79 

80 

81Licensing 

82--------- 

83Copyright 2013-2024 Python Charmers, Australia. 

84The software is distributed under an MIT licence. See LICENSE.txt. 

85""" 

86 

87from future import __version__, __copyright__, __license__ 

88 

89__title__ = 'past' 

90__author__ = 'Ed Schofield'