|
17 | 17 | * binary_type: str in Python 2, bythes in Python 3
|
18 | 18 | * string_types: basestring in Python 2, str in Python 3
|
19 | 19 | * 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) |
20 | 22 |
|
21 | 23 | Python 2.6 compatibility:
|
22 | 24 | * OrderedDict
|
|
34 | 36 | import types
|
35 | 37 |
|
36 | 38 | PY3 = (sys.version_info[0] >= 3)
|
37 |
| -# import iterator versions of these functions |
38 | 39 |
|
39 | 40 | try:
|
40 | 41 | import __builtin__ as builtins
|
@@ -96,6 +97,7 @@ def str_to_bytes(s, encoding='ascii'):
|
96 | 97 | def bytes_to_str(b, encoding='ascii'):
|
97 | 98 | return b
|
98 | 99 |
|
| 100 | + # import iterator versions of these functions |
99 | 101 | range = xrange
|
100 | 102 | zip = itertools.izip
|
101 | 103 | filter = itertools.ifilter
|
@@ -196,6 +198,19 @@ def u(s):
|
196 | 198 | def callable(obj):
|
197 | 199 | return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
|
198 | 200 |
|
| 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 | + |
199 | 214 | # ----------------------------------------------------------------------------
|
200 | 215 | # Python 2.6 compatibility shims
|
201 | 216 | #
|
|
0 commit comments