1
1
"""
2
2
A selection of cross-compatible functions for Python 2 and 3.
3
3
4
- This exports useful functions for 2/3 compatible code that are not
5
- builtins on Python 3:
4
+ This module exports useful functions for 2/3 compatible code:
6
5
7
6
* bind_method: binds functions to classes
8
7
* ``native_str_to_bytes`` and ``bytes_to_native_str``
9
8
* ``native_str``: always equal to the native platform string object (because
10
9
this may be shadowed by imports from future.builtins)
11
10
* lists: lrange(), lmap(), lzip(), lfilter()
12
- * iterable method compatibility: iteritems, iterkeys, itervalues
11
+ * iterable method compatibility:
12
+ - iteritems, iterkeys, itervalues
13
+ - viewitems, viewkeys, viewvalues
13
14
14
- * Uses the original method if available, otherwise uses items, keys, values.
15
+ These use the original method if available, otherwise they use items,
16
+ keys, values.
15
17
16
18
* types:
17
19
26
28
* tobytes(s)
27
29
Take a text string, a byte string, or a sequence of characters taken
28
30
from a byte string, and make a byte string.
29
-
30
- This module also defines a simple decorator called
31
- ``python_2_unicode_compatible`` (from django.utils.encoding) which
32
- defines ``__unicode__`` and ``__str__`` methods consistently under Python
33
- 3 and 2. To support Python 3 and 2 with a single code base, simply define
34
- a ``__str__`` method returning unicode text and apply the
35
- python_2_unicode_compatible decorator to the class like this::
36
-
37
- >>> from future.utils import python_2_unicode_compatible
38
-
39
- >>> @python_2_unicode_compatible
40
- ... class MyClass(object):
41
- ... def __str__(self):
42
- ... return u'Unicode string: \u5b54 \u5b50 '
43
31
44
- >>> a = MyClass()
32
+ * raise_from()
33
+ * raise_with_traceback()
45
34
46
- Then, after this import :
35
+ This module also defines these decorators :
47
36
48
- >>> from future.builtins import str
49
-
50
- the following is ``True`` on both Python 3 and 2::
51
-
52
- >>> str(a) == a.encode('utf-8').decode('utf-8')
53
- True
54
-
55
- and, on a Unicode-enabled terminal with the right fonts, these both print the
56
- Chinese characters for Confucius::
57
-
58
- print(a)
59
- print(str(a))
60
-
61
- On Python 3, this decorator is a no-op.
37
+ * ``python_2_unicode_compatible``
38
+ * ``with_metaclass``
39
+ * ``implements_iterator``
62
40
63
41
Some of the functions in this module come from the following sources:
64
42
87
65
def python_2_unicode_compatible (cls ):
88
66
"""
89
67
A decorator that defines __unicode__ and __str__ methods under Python
90
- 2. Under Python 3 it does nothing .
68
+ 2. Under Python 3, this decorator is a no-op .
91
69
92
70
To support Python 2 and 3 with a single code base, define a __str__
93
- method returning unicode text and apply this decorator to the class.
71
+ method returning unicode text and apply this decorator to the class, like
72
+ this::
73
+
74
+ >>> from future.utils import python_2_unicode_compatible
75
+
76
+ >>> @python_2_unicode_compatible
77
+ ... class MyClass(object):
78
+ ... def __str__(self):
79
+ ... return u'Unicode string: \u5b54 \u5b50 '
80
+
81
+ >>> a = MyClass()
82
+
83
+ Then, after this import:
84
+
85
+ >>> from future.builtins import str
86
+
87
+ the following is ``True`` on both Python 3 and 2::
88
+
89
+ >>> str(a) == a.encode('utf-8').decode('utf-8')
90
+ True
91
+
92
+ and, on a Unicode-enabled terminal with the right fonts, these both print the
93
+ Chinese characters for Confucius::
94
+
95
+ >>> print(a)
96
+ >>> print(str(a))
94
97
95
98
The implementation comes from django.utils.encoding.
96
99
"""
@@ -135,7 +138,7 @@ def __new__(cls, name, this_bases, d):
135
138
return metaclass ('temporary_class' , None , {})
136
139
137
140
138
- # Definitions from pandas.compat follow:
141
+ # Definitions from pandas.compat and six.py follow:
139
142
if PY3 :
140
143
def bchr (s ):
141
144
return bytes ([s ])
@@ -146,6 +149,13 @@ def bstr(s):
146
149
return bytes (s )
147
150
def bord (s ):
148
151
return s
152
+
153
+ string_types = str ,
154
+ integer_types = int ,
155
+ class_types = type ,
156
+ text_type = str
157
+ binary_type = bytes
158
+
149
159
else :
150
160
# Python 2
151
161
def bchr (s ):
@@ -155,6 +165,12 @@ def bstr(s):
155
165
def bord (s ):
156
166
return ord (s )
157
167
168
+ string_types = basestring ,
169
+ integer_types = (int , long )
170
+ class_types = (type , types .ClassType )
171
+ text_type = unicode
172
+ binary_type = str
173
+
158
174
###
159
175
160
176
if PY3 :
0 commit comments