1
|
|
|
import enum |
|
|
|
|
2
|
|
|
from typing import Optional, Union, AbstractSet |
3
|
|
|
|
4
|
|
|
from pocketutils.core.exceptions import XValueError |
|
|
|
|
5
|
|
|
|
6
|
|
|
from mandos.model.utils.setup import logger |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class DisjointEnum(enum.Enum): |
|
|
|
|
10
|
|
|
@classmethod |
11
|
|
|
def _fix_lookup(cls, s: str) -> str: |
|
|
|
|
12
|
|
|
return s |
13
|
|
|
|
14
|
|
|
@classmethod |
15
|
|
|
def or_none(cls, s: Union[str, __qualname__]) -> Optional[__qualname__]: |
|
|
|
|
16
|
|
|
try: |
17
|
|
|
return cls.of(s) |
18
|
|
|
except KeyError: |
19
|
|
|
return None |
20
|
|
|
|
21
|
|
|
@classmethod |
22
|
|
|
def of(cls, s: Union[str, __qualname__]) -> __qualname__: |
|
|
|
|
23
|
|
|
if isinstance(s, cls): |
24
|
|
|
return s |
25
|
|
|
return cls[cls._fix_lookup(s)] |
26
|
|
|
|
27
|
|
|
def __new__(cls, *args, **kwargs): |
|
|
|
|
28
|
|
|
value = len(cls.__members__) + 1 |
29
|
|
|
obj = object.__new__(cls) |
30
|
|
|
obj._value_ = value |
31
|
|
|
return obj |
32
|
|
|
|
33
|
|
|
def __repr__(self): |
|
|
|
|
34
|
|
|
return self.name |
35
|
|
|
|
36
|
|
|
def __str__(self): |
|
|
|
|
37
|
|
|
return self.name |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class FlagEnum(enum.Flag): |
41
|
|
|
""" |
42
|
|
|
A bit flag that behaves as a set, has a null set, and auto-sets values and names. |
43
|
|
|
|
44
|
|
|
Example: |
45
|
|
|
.. code-block:: |
46
|
|
|
class Flavor(FlagEnum): |
47
|
|
|
none = () |
48
|
|
|
bitter = () |
49
|
|
|
sweet = () |
50
|
|
|
sour = () |
51
|
|
|
umami = () |
52
|
|
|
bittersweet = Flavor.bitter | Flavor.sweet |
53
|
|
|
print(bittersweet.value) # 1 + 2 == 3 |
54
|
|
|
print(bittersweet.name) # "bitter|sweet" |
55
|
|
|
|
56
|
|
|
.. important:: |
57
|
|
|
The *first element* must always be the null set ("no flags") |
58
|
|
|
and should be named something like 'none', 'empty', or 'zero' |
59
|
|
|
""" |
60
|
|
|
|
61
|
|
|
@classmethod |
62
|
|
|
def _fix_lookup(cls, s: str) -> str: |
|
|
|
|
63
|
|
|
return s |
64
|
|
|
|
65
|
|
|
def __new__(cls, *args, **kwargs): |
|
|
|
|
66
|
|
|
if len(cls.__members__) == 0: |
67
|
|
|
value = 0 |
68
|
|
|
else: |
69
|
|
|
value = 2 ** (len(cls.__members__) - 1) |
70
|
|
|
obj = object.__new__(cls) |
71
|
|
|
obj._value_ = value |
72
|
|
|
return obj |
73
|
|
|
|
74
|
|
|
@classmethod |
75
|
|
|
def _create_pseudo_member_(cls, value): |
76
|
|
|
value = super()._create_pseudo_member_(value) |
77
|
|
|
members, _ = enum._decompose(cls, value) |
|
|
|
|
78
|
|
|
value._name_ = "|".join([m.name for m in members]) |
|
|
|
|
79
|
|
|
return value |
80
|
|
|
|
81
|
|
|
@classmethod |
82
|
|
|
def or_none(cls, s: Union[str, __qualname__]) -> Optional[__qualname__]: |
|
|
|
|
83
|
|
|
try: |
84
|
|
|
return cls.of(s) |
85
|
|
|
except KeyError: |
86
|
|
|
return None |
87
|
|
|
|
88
|
|
|
@classmethod |
89
|
|
|
def of(cls, s: Union[str, __qualname__, AbstractSet[Union[str, __qualname__]]]) -> __qualname__: |
|
|
|
|
90
|
|
|
if isinstance(s, cls): |
91
|
|
|
return s |
92
|
|
|
if isinstance(s, str): |
93
|
|
|
return cls[cls._fix_lookup_(s)] |
|
|
|
|
94
|
|
|
z = cls[0] |
|
|
|
|
95
|
|
|
for m in s: |
|
|
|
|
96
|
|
|
z |= cls.of(m) |
|
|
|
|
97
|
|
|
return z |
98
|
|
|
|
99
|
|
|
def __repr__(self): |
|
|
|
|
100
|
|
|
return self.name |
101
|
|
|
|
102
|
|
|
def __str__(self): |
|
|
|
|
103
|
|
|
return self.name |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
class TrueFalseUnknown(DisjointEnum): |
|
|
|
|
107
|
|
|
true = () |
108
|
|
|
false = () |
109
|
|
|
unknown = () |
110
|
|
|
|
111
|
|
|
@classmethod |
112
|
|
|
def _unmatched_type(cls) -> Optional[__qualname__]: |
113
|
|
|
return cls.unknown |
114
|
|
|
|
115
|
|
|
@classmethod |
116
|
|
|
def _fix_lookup(cls, s: str) -> str: |
117
|
|
|
s = s.lower().strip() |
118
|
|
|
return dict(t="true", false="false").get(s, s) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
class MultiTruth(FlagEnum): |
|
|
|
|
122
|
|
|
false = () |
123
|
|
|
true = () |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
class CleverEnum(DisjointEnum): |
127
|
|
|
""" |
128
|
|
|
An enum with a ``.of`` method that finds values |
129
|
|
|
with limited string/value fixing. |
130
|
|
|
May support an "unmatched" type -- a fallback value when there is no match. |
131
|
|
|
This is similar to pocketutils' simpler ``SmartEnum``. |
132
|
|
|
It is mainly useful for enums corresponding to concepts in ChEMBL and PubChem, |
133
|
|
|
where it's acceptable for the user to input spaces (like the database concepts use) |
134
|
|
|
rather than the underscores that Python requires. |
135
|
|
|
""" |
136
|
|
|
|
137
|
|
|
@classmethod |
138
|
|
|
def of(cls, s: Union[str, __qualname__]) -> __qualname__: |
139
|
|
|
try: |
140
|
|
|
return super().of(s) |
141
|
|
|
except KeyError: |
142
|
|
|
unknown = cls._unmatched_type() |
143
|
|
|
logger.error(f"Value {s} not found. Using {unknown}") |
144
|
|
|
if unknown is None: |
145
|
|
|
raise XValueError(f"Value {s} not found and unmatched_type is None") |
146
|
|
|
return unknown |
147
|
|
|
|
148
|
|
|
@classmethod |
149
|
|
|
def _unmatched_type(cls) -> Optional[__qualname__]: |
150
|
|
|
return None |
151
|
|
|
|
152
|
|
|
@classmethod |
153
|
|
|
def _fix_lookup(cls, s: str) -> str: |
154
|
|
|
return s.strip().replace(" ", "_").replace(".", "_").replace("-", "_").lower() |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
__all__ = ["TrueFalseUnknown", "DisjointEnum", "FlagEnum", "CleverEnum"] |
158
|
|
|
|