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: range(), map(), zip(), filter(), reduce()
8
+ * iterators: reduce()
9
9
* lists: lrange(), lmap(), lzip(), lfilter()
10
10
* unicode: u() [no unicode builtin in Python 3]
11
11
* longs: long (int in Python 3)
@@ -107,19 +107,11 @@ def get_range_parameters(data):
107
107
return data .start , data .stop , data .step
108
108
109
109
# have to explicitly put builtins into the namespace
110
- range = range
111
- map = map
112
- zip = zip
113
- filter = filter
114
110
intern = sys .intern
115
111
reduce = functools .reduce
116
112
long = int
117
113
unichr = chr
118
114
119
- # This was introduced in Python 3.3, but we don't support
120
- # Python 3.x < 3.5, so checking PY3 is safe.
121
- FileNotFoundError = FileNotFoundError
122
-
123
115
# list-producing versions of the major Python iterating functions
124
116
def lrange (* args , ** kwargs ):
125
117
return list (range (* args , ** kwargs ))
@@ -148,8 +140,6 @@ def lfilter(*args, **kwargs):
148
140
# Python 2
149
141
_name_re = re .compile (r"[a-zA-Z_][a-zA-Z0-9_]*$" )
150
142
151
- FileNotFoundError = IOError
152
-
153
143
def isidentifier (s , dotted = False ):
154
144
return bool (_name_re .match (s ))
155
145
@@ -181,11 +171,7 @@ def get_range_parameters(data):
181
171
return start , stop , step
182
172
183
173
# import iterator versions of these functions
184
- range = xrange
185
174
intern = intern
186
- zip = itertools .izip
187
- filter = itertools .ifilter
188
- map = itertools .imap
189
175
reduce = reduce
190
176
long = long
191
177
unichr = unichr
@@ -217,7 +203,6 @@ def iterkeys(obj, **kw):
217
203
def itervalues (obj , ** kw ):
218
204
return obj .itervalues (** kw )
219
205
220
- next = lambda it : it .next ()
221
206
else :
222
207
def iteritems (obj , ** kw ):
223
208
return iter (obj .items (** kw ))
@@ -228,8 +213,6 @@ def iterkeys(obj, **kw):
228
213
def itervalues (obj , ** kw ):
229
214
return iter (obj .values (** kw ))
230
215
231
- next = next
232
-
233
216
234
217
def bind_method (cls , name , func ):
235
218
"""Bind a method to class, python 2 and python 3 compatible.
@@ -315,9 +298,6 @@ def set_function_name(f, name, cls):
315
298
name = name )
316
299
f .__module__ = cls .__module__
317
300
return f
318
-
319
- ResourceWarning = ResourceWarning
320
-
321
301
else :
322
302
string_types = basestring ,
323
303
integer_types = (int , long )
@@ -373,9 +353,6 @@ def set_function_name(f, name, cls):
373
353
f .__name__ = name
374
354
return f
375
355
376
- class ResourceWarning (Warning ):
377
- pass
378
-
379
356
string_and_binary_types = string_types + (binary_type ,)
380
357
381
358
0 commit comments