Skip to content

Commit c0198b4

Browse files
committed
ENH: Add 'add_metaclass' class decorator.
Taken from a new addition to the six library (again, license is in the LICENSE directory).
1 parent 1409049 commit c0198b4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pandas/compat/__init__.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* binary_type: str in Python 2, bythes in Python 3
1818
* string_types: basestring in Python 2, str in Python 3
1919
* bind_method: binds functions to classes
20+
* add_metaclass(metaclass) - class decorator that recreates class with with the
21+
given metaclass instead (and avoids intermediary class creation)
2022
2123
Python 2.6 compatibility:
2224
* OrderedDict
@@ -34,7 +36,6 @@
3436
import types
3537

3638
PY3 = (sys.version_info[0] >= 3)
37-
# import iterator versions of these functions
3839

3940
try:
4041
import __builtin__ as builtins
@@ -96,6 +97,7 @@ def str_to_bytes(s, encoding='ascii'):
9697
def bytes_to_str(b, encoding='ascii'):
9798
return b
9899

100+
# import iterator versions of these functions
99101
range = xrange
100102
zip = itertools.izip
101103
filter = itertools.ifilter
@@ -196,6 +198,19 @@ def u(s):
196198
def callable(obj):
197199
return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
198200

201+
202+
def add_metaclass(metaclass):
203+
"""Class decorator for creating a class with a metaclass."""
204+
def wrapper(cls):
205+
orig_vars = cls.__dict__.copy()
206+
orig_vars.pop('__dict__', None)
207+
orig_vars.pop('__weakref__', None)
208+
for slots_var in orig_vars.get('__slots__', ()):
209+
orig_vars.pop(slots_var)
210+
return metaclass(cls.__name__, cls.__bases__, orig_vars)
211+
return wrapper
212+
213+
199214
# ----------------------------------------------------------------------------
200215
# Python 2.6 compatibility shims
201216
#

0 commit comments

Comments
 (0)