5
5
Cross-compatible functions for Python 2 and 3.
6
6
7
7
Key items to import for 2/3 compatible code:
8
- * iterators: reduce()
9
8
* lists: lrange(), lmap(), lzip(), lfilter()
10
9
* iterable method compatibility: iteritems, iterkeys, itervalues
11
10
* Uses the original method if available, otherwise uses items, keys, values.
@@ -100,15 +99,6 @@ def signature(f):
100
99
'varargs' , 'keywords' ])
101
100
return argspec (args , defaults , varargs , keywords )
102
101
103
- def get_range_parameters (data ):
104
- """Gets the start, stop, and step parameters from a range object"""
105
- return data .start , data .stop , data .step
106
-
107
- # have to explicitly put builtins into the namespace
108
- intern = sys .intern
109
- reduce = functools .reduce
110
- unichr = chr
111
-
112
102
# list-producing versions of the major Python iterating functions
113
103
def lrange (* args , ** kwargs ):
114
104
return list (range (* args , ** kwargs ))
@@ -122,8 +112,6 @@ def lmap(*args, **kwargs):
122
112
def lfilter (* args , ** kwargs ):
123
113
return list (filter (* args , ** kwargs ))
124
114
125
- from importlib import reload
126
- reload = reload
127
115
Hashable = collections .abc .Hashable
128
116
Iterable = collections .abc .Iterable
129
117
Iterator = collections .abc .Iterator
@@ -149,37 +137,12 @@ def bytes_to_str(b, encoding='ascii'):
149
137
def signature (f ):
150
138
return inspect .getargspec (f )
151
139
152
- def get_range_parameters (data ):
153
- """Gets the start, stop, and step parameters from a range object"""
154
- # seems we only have indexing ops to infer
155
- # rather than direct accessors
156
- if len (data ) > 1 :
157
- step = data [1 ] - data [0 ]
158
- stop = data [- 1 ] + step
159
- start = data [0 ]
160
- elif len (data ):
161
- start = data [0 ]
162
- stop = data [0 ] + 1
163
- step = 1
164
- else :
165
- start = stop = 0
166
- step = 1
167
-
168
- return start , stop , step
169
-
170
- # import iterator versions of these functions
171
- intern = intern
172
- reduce = reduce
173
- unichr = unichr
174
-
175
140
# Python 2-builtin ranges produce lists
176
141
lrange = builtins .range
177
142
lzip = builtins .zip
178
143
lmap = builtins .map
179
144
lfilter = builtins .filter
180
145
181
- reload = builtins .reload
182
-
183
146
Hashable = collections .Hashable
184
147
Iterable = collections .Iterable
185
148
Iterator = collections .Iterator
@@ -247,7 +210,6 @@ class to receive bound method
247
210
248
211
if PY3 :
249
212
string_types = str ,
250
- class_types = type ,
251
213
text_type = str
252
214
binary_type = bytes
253
215
@@ -274,11 +236,6 @@ def east_asian_len(data, encoding=None, ambiguous_width=1):
274
236
else :
275
237
return len (data )
276
238
277
- def import_lzma ():
278
- """ import lzma from the std library """
279
- import lzma
280
- return lzma
281
-
282
239
def set_function_name (f , name , cls ):
283
240
""" Bind the name/qualname attributes of the function """
284
241
f .__name__ = name
@@ -289,7 +246,6 @@ def set_function_name(f, name, cls):
289
246
return f
290
247
else :
291
248
string_types = basestring ,
292
- class_types = (type , types .ClassType )
293
249
text_type = unicode
294
250
binary_type = str
295
251
@@ -321,12 +277,6 @@ def east_asian_len(data, encoding=None, ambiguous_width=1):
321
277
else :
322
278
return len (data )
323
279
324
- def import_lzma ():
325
- """ import the backported lzma library
326
- or raise ImportError if not available """
327
- from backports import lzma
328
- return lzma
329
-
330
280
def set_function_name (f , name , cls ):
331
281
""" Bind the name attributes of the function """
332
282
f .__name__ = name
@@ -335,20 +285,6 @@ def set_function_name(f, name, cls):
335
285
string_and_binary_types = string_types + (binary_type ,)
336
286
337
287
338
- if PY2 :
339
- # In PY2 functools.wraps doesn't provide metadata pytest needs to generate
340
- # decorated tests using parametrization. See pytest GH issue #2782
341
- def wraps (wrapped , assigned = functools .WRAPPER_ASSIGNMENTS ,
342
- updated = functools .WRAPPER_UPDATES ):
343
- def wrapper (f ):
344
- f = functools .wraps (wrapped , assigned , updated )(f )
345
- f .__wrapped__ = wrapped
346
- return f
347
- return wrapper
348
- else :
349
- wraps = functools .wraps
350
-
351
-
352
288
def add_metaclass (metaclass ):
353
289
"""Class decorator for creating a class with a metaclass."""
354
290
def wrapper (cls ):
0 commit comments