-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasy_enum.py
99 lines (83 loc) · 3.23 KB
/
easy_enum.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
# Copyright 2019 Martin Olejar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = "Martin Olejar"
__contact__ = "martin.olejar@gmail.com"
__version__ = "0.3.0"
__license__ = "Apache 2.0"
__status__ = "Production"
__all__ = ['Enum']
class MetaEnum(type):
""" Meta Class for Enum Type """
def __new__(mcs, name, bases, attrs):
cls = super().__new__(mcs, name, bases, attrs)
cls._items_ = list()
for attr, value in attrs.items():
if attr in set(dir(type(name, (object,), {}))) or (attr.startswith('_') and attr.endswith('_')):
continue
if isinstance(value, classmethod):
continue
if isinstance(value, tuple):
if len(value) == 2:
cls._items_.append((attr, value[0], value[1]))
else:
cls._items_.append((value[1], value[0], value[2]))
setattr(cls, attr, value[0])
else:
cls._items_.append((attr, value, ''))
return cls
def __getitem__(cls, key):
if isinstance(key, str):
for name, value, _ in cls._items_:
if key.upper() == name.upper():
return value
raise KeyError("\'%s\' has no item with name \'%s\'" % (cls.__name__, key))
elif isinstance(key, int):
for name, value, _ in cls._items_:
if key == value:
return name
raise KeyError("\'%s\' has no item with value \'%d\'" % (cls.__name__, key))
else:
raise TypeError("\'%s\' has no item with type \'%r\'" % (cls.__name__, type(key)))
def __iter__(cls):
return (item for item in cls._items_)
def __contains__(cls, item):
if isinstance(item, str) and item in (item[0] for item in cls._items_):
return True
if isinstance(item, int) and item in (item[1] for item in cls._items_):
return True
return False
def __len__(cls):
return len(cls._items_)
class Enum(metaclass=MetaEnum):
""" Enum Type Class """
@classmethod
def get(cls, key, default=None):
try:
return cls[key]
except KeyError:
return default
@classmethod
def desc(cls, key, default=''):
if isinstance(key, str):
for name, _, desc in cls._items_:
if key.upper() == name.upper():
return desc
return default
elif isinstance(key, int):
for _, value, desc in cls._items_:
if key == value:
return desc
return default
else:
raise TypeError("\'%s\' has no item with type \'%r\'" % (cls.__name__, type(key)))