Coverage for C:\Users\hjanssen\HOME\pyCharmProjects\ethz_hvl\hvl_ccb\hvl_ccb\utils\enum.py : 62%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (c) 2019-2020 ETH Zurich, SIS ID and HVL D-ITET
2#
4import aenum
7# Use abstract base class instead of Mixin to inherit from `aenum.Enum` to make Sphinx
8# detect inheritance correctly and create docs for derived enums, including such as
9# these in `dev.supercube.constants`. With Mixin approach, module-level enum classes
10# are not documented.
11class StrEnumBase(aenum.Enum):
12 """
13 String representation-based equality and lookup.
14 """
16 def __eq__(self, other):
17 return (self is other) or (other.__eq__(str(self)))
19 # use only with aenum enums
20 @classmethod
21 def _missing_value_(cls, value):
22 for member in cls:
23 if member == value:
24 return member
26 def __str__(self):
27 raise NotImplementedError
29 def __hash__(self):
30 return hash(str(self))
33unique = aenum.unique
36class ValueEnum(StrEnumBase):
37 """
38 Enum with string representation of values used as string representation, and with
39 lookup and equality based on this representation.
41 Attention: to avoid errors, best use together with `unique` enum decorator.
42 """
44 def __str__(self):
45 return str(self.value)
48class NameEnum(StrEnumBase):
49 """
50 Enum with names used as string representation, and with lookup and equality based on
51 this representation.
52 """
54 # convenience: enables `[]` name-based lookup with enum instances themselves
55 def __hash__(self):
56 return hash(str(self))
58 def __str__(self):
59 return self.name
62class AutoNumberNameEnum(NameEnum, aenum.AutoNumberEnum):
63 """
64 Auto-numbered enum with names used as string representation, and with lookup and
65 equality based on this representation.
66 """
68 pass