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
- * bind_method: binds functions to classes
12
11
* add_metaclass(metaclass) - class decorator that recreates class with with the
13
12
given metaclass instead (and avoids intermediary class creation)
14
13
22
21
from distutils .version import LooseVersion
23
22
import sys
24
23
import platform
25
- import types
26
24
import struct
27
- import inspect
28
- from collections import namedtuple
29
25
30
26
PY2 = sys .version_info [0 ] == 2
31
27
PY3 = sys .version_info [0 ] >= 3
34
30
PY37 = sys .version_info >= (3 , 7 )
35
31
PYPY = platform .python_implementation () == 'PyPy'
36
32
37
- from pandas .compat .chainmap import DeepChainMap
38
-
39
33
40
34
# list-producing versions of the major Python iterating functions
41
35
def lrange (* args , ** kwargs ):
@@ -54,58 +48,6 @@ def lfilter(*args, **kwargs):
54
48
return list (filter (* args , ** kwargs ))
55
49
56
50
57
- if PY3 :
58
- def isidentifier (s ):
59
- return s .isidentifier ()
60
-
61
- def str_to_bytes (s , encoding = None ):
62
- return s .encode (encoding or 'ascii' )
63
-
64
- def bytes_to_str (b , encoding = None ):
65
- return b .decode (encoding or 'utf-8' )
66
-
67
- # The signature version below is directly copied from Django,
68
- # https://github.com/django/django/pull/4846
69
- def signature (f ):
70
- sig = inspect .signature (f )
71
- args = [
72
- p .name for p in sig .parameters .values ()
73
- if p .kind == inspect .Parameter .POSITIONAL_OR_KEYWORD
74
- ]
75
- varargs = [
76
- p .name for p in sig .parameters .values ()
77
- if p .kind == inspect .Parameter .VAR_POSITIONAL
78
- ]
79
- varargs = varargs [0 ] if varargs else None
80
- keywords = [
81
- p .name for p in sig .parameters .values ()
82
- if p .kind == inspect .Parameter .VAR_KEYWORD
83
- ]
84
- keywords = keywords [0 ] if keywords else None
85
- defaults = [
86
- p .default for p in sig .parameters .values ()
87
- if p .kind == inspect .Parameter .POSITIONAL_OR_KEYWORD
88
- and p .default is not p .empty
89
- ] or None
90
- argspec = namedtuple ('Signature' , ['args' , 'defaults' ,
91
- 'varargs' , 'keywords' ])
92
- return argspec (args , defaults , varargs , keywords )
93
- else :
94
- # Python 2
95
- _name_re = re .compile (r"[a-zA-Z_][a-zA-Z0-9_]*$" )
96
-
97
- def isidentifier (s , dotted = False ):
98
- return bool (_name_re .match (s ))
99
-
100
- def str_to_bytes (s , encoding = 'ascii' ):
101
- return s
102
-
103
- def bytes_to_str (b , encoding = 'ascii' ):
104
- return b
105
-
106
- def signature (f ):
107
- return inspect .getargspec (f )
108
-
109
51
if PY2 :
110
52
def iteritems (obj , ** kw ):
111
53
return obj .iteritems (** kw )
@@ -126,30 +68,6 @@ def iterkeys(obj, **kw):
126
68
def itervalues (obj , ** kw ):
127
69
return iter (obj .values (** kw ))
128
70
129
-
130
- def bind_method (cls , name , func ):
131
- """Bind a method to class, python 2 and python 3 compatible.
132
-
133
- Parameters
134
- ----------
135
-
136
- cls : type
137
- class to receive bound method
138
- name : basestring
139
- name of method on class instance
140
- func : function
141
- function to be bound as method
142
-
143
-
144
- Returns
145
- -------
146
- None
147
- """
148
- # only python 2 has bound/unbound method issue
149
- if not PY3 :
150
- setattr (cls , name , types .MethodType (func , None , cls ))
151
- else :
152
- setattr (cls , name , func )
153
71
# ----------------------------------------------------------------------------
154
72
# functions largely based / taken from the six module
155
73
@@ -164,7 +82,7 @@ def to_str(s):
164
82
Convert bytes and non-string into Python 3 str
165
83
"""
166
84
if isinstance (s , bytes ):
167
- s = bytes_to_str ( s )
85
+ s = s . decode ( 'utf-8' )
168
86
elif not isinstance (s , str ):
169
87
s = str (s )
170
88
return s
@@ -203,6 +121,7 @@ def wrapper(cls):
203
121
return metaclass (cls .__name__ , cls .__bases__ , orig_vars )
204
122
return wrapper
205
123
124
+
206
125
if PY3 :
207
126
def raise_with_traceback (exc , traceback = Ellipsis ):
208
127
if traceback == Ellipsis :
@@ -238,6 +157,7 @@ def raise_with_traceback(exc, traceback=Ellipsis):
238
157
else :
239
158
re_type = type (re .compile ('' ))
240
159
160
+
241
161
# https://github.com/pandas-dev/pandas/pull/9123
242
162
def is_platform_little_endian ():
243
163
""" am I little endian """
0 commit comments