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

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

## 24 statements

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#

3

4 import aenum

5

6

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.

11 class StrEnumBase(aenum.Enum):

12 """

13 String representation-based equality and lookup.

14 """

15

16 def __eq__(self, other):

17 return (self is other) or (other.__eq__(str(self)))

18

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

25

26 def __str__(self):

27 raise NotImplementedError

28

29 def __hash__(self):

30 return hash(str(self))

31

32

33 unique = aenum.unique

34

35

36 class ValueEnum(StrEnumBase):

37 """

38 Enum with string representation of values used as string representation, and with

39 lookup and equality based on this representation.

40

41 Attention: to avoid errors, best use together with `unique` enum decorator.

42 """

43

44 def __str__(self):

45 return str(self.value)

46

47

48 class NameEnum(StrEnumBase):

49 """

50 Enum with names used as string representation, and with lookup and equality based on

51 this representation.

52 """

53

54 # convenience: enables `[]` name-based lookup with enum instances themselves

55 def __hash__(self):

56 return hash(str(self))

57

58 def __str__(self):

59 return self.name

60

61

62 class 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 """

67

68 pass