-
Notifications
You must be signed in to change notification settings - Fork 5
/
named_constants.py
178 lines (134 loc) · 5.47 KB
/
named_constants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sys, inspect
#---------------------------------------------------------------------
# python 2 / 3 compatibility stuff
PY2 = sys.version_info <= (3,)
def with_metaclass(meta, *bases):
"""
Function from jinja2/_compat.py. License: BSD.
Use it like this::
class BaseForm(object):
pass
class FormType(type):
pass
class Form(with_metaclass(FormType, BaseForm)):
pass
This requires a bit of explanation: the basic idea is to make a
dummy metaclass for one level of class instantiation that replaces
itself with the actual metaclass. Because of internal type checks
we also need to make sure that we downgrade the custom metaclass
for one level to something closer to type (that's why __call__ and
__init__ comes back from type etc.).
This has the advantage over six.with_metaclass of not introducing
dummy classes into the final MRO.
"""
class metaclass(meta):
__call__ = type.__call__
__init__ = type.__init__
def __new__(cls, name, this_bases, d):
if this_bases is None:
return type.__new__(cls, name, (), d)
return meta(name, bases, d)
return metaclass('temporary_class', None, {})
#---------------------------------------------------------------------
class _ConstantsMeta(type):
__NamedTypes = {}
@classmethod
def NamedValue(cls, typ):
"""Returns a 'NamedTyp' class derived from the given 'typ'.
The results are cached, i.e. given the same type, the same
class will be returned in subsequent calls."""
Const = cls.__NamedTypes.get(typ, None)
if Const is None:
def __new__(cls, name, value):
res = typ.__new__(cls, value)
res._name = name
res._namespace = None
return res
def name(self):
return self._name
def __repr__(self):
if self._namespace is None:
return self._name
if self._namespace.__module__ in ('__main__', '__builtin__'):
namespace = self._namespace.__name__
else:
namespace = "%s.%s" % (self._namespace.__module__,
self._namespace.__name__)
return "%s.%s" % (namespace, self._name)
dct = dict(
__doc__ = """
Named, typed constant (subclassed from original type, cf. `Constants`
class). Sole purpose is pretty-printing, i.e. __repr__ returns the
constant's name instead of the original string representations.
The name is also available via a `name()` method.""".lstrip(),
__new__ = __new__,
name = name,
#__str__ = name,
__repr__ = __repr__)
typName = typ.__name__
name = 'Named' + typName[0].upper() + typName[1:]
Const = type(name, (typ, ), dct)
cls.__NamedTypes[typ] = Const
return Const
def __new__(cls, name, bases, dct):
constants = {}
# replace class contents with values wrapped in (typed) Const-class:
for member in dct:
value = dct[member]
if member.startswith('_') or inspect.isfunction(value) or inspect.ismethoddescriptor(value):
continue
Const = cls.NamedValue(type(value))
c = Const(member, value)
constants[member] = c
dct[member] = c
dct['__constants__'] = constants
dct['__reverse__'] = dict((value, value) for key, value in constants.items())
dct['__sorted__'] = sorted(constants.values(), key = lambda x: (id(type(x)), x))
result = type.__new__(cls, name, bases, dct)
# support namespace prefix in __repr__ by connecting the namespace here:
for c in constants.values():
c._namespace = result
return result
def __len__(self):
return len(self.__constants__)
def __iter__(self):
return iter(self.__sorted__)
def __setattr__(self, _name, _value):
raise TypeError('Constants are not supposed to be changed ex post')
def __contains__(self, x):
return self.has_key(x) or self.has_value(x)
def has_key(self, key):
return key in self.__constants__
def has_value(self, value):
return value in self.__reverse__
def keys(self):
for c in self.__sorted__:
yield c.name()
def values(self):
return self.__sorted__
def items(self):
for c in self.__sorted__:
yield c.name(), c
if PY2:
iterkeys = keys
itervalues = values
iteritems = items
def keys(self):
return [c.name() for c in self.__sorted__]
def values(self):
return self.__sorted__
def items(self):
return [(c.name(), c) for c in self.__sorted__]
class Constants(with_metaclass(_ConstantsMeta)):
"""Base class for constant namespaces."""
__slots__ = ()
def __new__(cls, x):
if cls.has_value(x):
return cls.__reverse__[x]
if cls.has_key(x):
return cls.__constants__[x]
raise ValueError('%s has no key or value %r' % (cls.__name__, x))
# --------------------------------------------------------------------
if __name__ == '__main__':
import doctest
doctest.testfile('README.rst')