1 | ## Based on PEP-354, copied and adapted from |
||
2 | ## http://stackoverflow.com/questions/36932/ |
||
3 | |||
4 | _enum_registry = {} |
||
5 | |||
6 | |||
7 | def enum_value_unpickler(names, value): |
||
8 | enum_class = _enum_registry.get(names, None) or Enum(*names) |
||
9 | return enum_class[value] |
||
10 | |||
11 | def Enum(*names): |
||
12 | """ |
||
13 | Constructs a class with named constants. The returned class serves as a |
||
14 | namespace, and adds support for getting items and iteration. Values are |
||
15 | instances of a class derived from `int` that can be printed out in symbolic |
||
16 | form. |
||
17 | |||
18 | :param names: names of constants |
||
19 | :return: class with named constants. |
||
20 | |||
21 | Typical use is:: |
||
22 | |||
23 | VarTypes = Enum("None", "Discrete", "Continuous", "String") |
||
24 | |||
25 | in `Orange.data.Variable`, which adds a namespace VarType to type Variable |
||
26 | to have constants `Orange.data.Variable.VarTypes.None`, |
||
27 | `Orange.data.Variable.VarTypes.Discrete` and so forth. |
||
28 | |||
29 | The resulting class has also a method `pull_up(self, cls)` that puts the |
||
30 | constants into the namespace of the given class `cls`. The following code |
||
31 | is used (in module scope) to add named constants to class |
||
32 | `FilterContinuous`:: |
||
33 | |||
34 | Enum("Equal", "NotEqual", "Less", "LessEqual", |
||
35 | "Greater", "GreaterEqual", "Between", "Outside", |
||
36 | "IsDefined").pull_up(FilterContinuous) |
||
37 | |||
38 | With this, the class `FilterContinuous` has constants |
||
39 | `FilterContinuous.Equal`, `FilterContinuous.NotEqual` and so forth, and not |
||
40 | `FilterContinuous.SomeNamespaceName.Equal`. |
||
41 | """ |
||
42 | if names in _enum_registry: |
||
43 | return _enum_registry[names] |
||
44 | |||
45 | class EnumClass: |
||
46 | __slots__ = names |
||
47 | |||
48 | def __iter__(self): |
||
49 | return iter(constants) |
||
50 | |||
51 | def __len__(self): |
||
52 | return len(constants) |
||
53 | |||
54 | def __getitem__(self, i): |
||
55 | return constants[i] |
||
56 | |||
57 | def __repr__(self): |
||
58 | return 'Enum' + str(names) |
||
59 | |||
60 | def __str__(self): |
||
61 | return 'enum ' + str(constants) |
||
62 | |||
63 | def add_value(self, value): |
||
64 | value = EnumValue(len(names)) |
||
65 | setattr(self, value, value) |
||
66 | constants.append(value) |
||
67 | |||
68 | def pull_up(self, cls): |
||
0 ignored issues
–
show
|
|||
69 | for name, value in zip(names, constants): |
||
70 | setattr(cls, name, value) |
||
71 | |||
72 | class EnumValue(int): |
||
73 | EnumType = property(lambda self: EnumType) |
||
74 | |||
75 | def __repr__(self): |
||
76 | return str(names[self]) |
||
77 | |||
78 | __str__ = __repr__ |
||
79 | |||
80 | def __reduce__(self): |
||
81 | return enum_value_unpickler, (names, int(self)) |
||
82 | |||
83 | constants = [None] * len(names) |
||
84 | for i, each in enumerate(names): |
||
85 | val = EnumValue(i) |
||
86 | setattr(EnumClass, each, val) |
||
87 | constants[i] = val |
||
88 | constants = tuple(constants) |
||
89 | EnumType = EnumClass() |
||
90 | _enum_registry[names] = EnumType |
||
91 | return EnumType |
||
92 |
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example
could be written as