8
8
* lists: lrange(), lmap(), lzip(), lfilter()
9
9
* iterable method compatibility: iteritems, iterkeys, itervalues
10
10
* Uses the original method if available, otherwise uses items, keys, values.
11
- * types:
12
- * text_type: unicode in Python 2, str in Python 3
13
- * binary_type: str in Python 2, bytes in Python 3
14
- * string_types: basestring in Python 2, str in Python 3
15
11
* bind_method: binds functions to classes
16
12
* add_metaclass(metaclass) - class decorator that recreates class with with the
17
13
given metaclass instead (and avoids intermediary class creation)
30
26
import sys
31
27
import platform
32
28
import types
33
- from unicodedata import east_asian_width
34
29
import struct
35
30
import inspect
36
31
from collections import namedtuple
@@ -203,39 +198,18 @@ class to receive bound method
203
198
# The license for this library can be found in LICENSES/SIX and the code can be
204
199
# found at https://bitbucket.org/gutworth/six
205
200
206
- # Definition of East Asian Width
207
- # http://unicode.org/reports/tr11/
208
- # Ambiguous width can be changed by option
209
- _EAW_MAP = {'Na' : 1 , 'N' : 1 , 'W' : 2 , 'F' : 2 , 'H' : 1 }
210
201
211
202
if PY3 :
212
- string_types = str ,
213
- text_type = str
214
- binary_type = bytes
215
-
216
203
def to_str (s ):
217
204
"""
218
205
Convert bytes and non-string into Python 3 str
219
206
"""
220
- if isinstance (s , binary_type ):
207
+ if isinstance (s , bytes ):
221
208
s = bytes_to_str (s )
222
- elif not isinstance (s , string_types ):
209
+ elif not isinstance (s , str ):
223
210
s = str (s )
224
211
return s
225
212
226
- def strlen (data , encoding = None ):
227
- # encoding is for compat with PY2
228
- return len (data )
229
-
230
- def east_asian_len (data , encoding = None , ambiguous_width = 1 ):
231
- """
232
- Calculate display width considering unicode East Asian Width
233
- """
234
- if isinstance (data , text_type ):
235
- return sum (_EAW_MAP .get (east_asian_width (c ), ambiguous_width ) for c in data )
236
- else :
237
- return len (data )
238
-
239
213
def set_function_name (f , name , cls ):
240
214
""" Bind the name/qualname attributes of the function """
241
215
f .__name__ = name
@@ -245,45 +219,19 @@ def set_function_name(f, name, cls):
245
219
f .__module__ = cls .__module__
246
220
return f
247
221
else :
248
- string_types = basestring ,
249
- text_type = unicode
250
- binary_type = str
251
-
252
222
def to_str (s ):
253
223
"""
254
224
Convert unicode and non-string into Python 2 str
255
225
"""
256
- if not isinstance (s , string_types ):
226
+ if not isinstance (s , basestring ):
257
227
s = str (s )
258
228
return s
259
229
260
- def strlen (data , encoding = None ):
261
- try :
262
- data = data .decode (encoding )
263
- except UnicodeError :
264
- pass
265
- return len (data )
266
-
267
- def east_asian_len (data , encoding = None , ambiguous_width = 1 ):
268
- """
269
- Calculate display width considering unicode East Asian Width
270
- """
271
- if isinstance (data , text_type ):
272
- try :
273
- data = data .decode (encoding )
274
- except UnicodeError :
275
- pass
276
- return sum (_EAW_MAP .get (east_asian_width (c ), ambiguous_width ) for c in data )
277
- else :
278
- return len (data )
279
-
280
230
def set_function_name (f , name , cls ):
281
231
""" Bind the name attributes of the function """
282
232
f .__name__ = name
283
233
return f
284
234
285
- string_and_binary_types = string_types + (binary_type ,)
286
-
287
235
288
236
def add_metaclass (metaclass ):
289
237
"""Class decorator for creating a class with a metaclass."""
0 commit comments