AutoEnum
class¶
- class enum_extend.AutoEnum(value)¶
Automatically numbers enum members starting from 1.
AutoEnum
inherits from EnumComparable class and thus support all EnumComparable operations.Can be used with operators
==
,!=
,<
,<=
,>
,>=
,+
,+=
,-
, and-=
.Values on the right side of the operator can be other Enum, number, str.
Includes support for a custom docstring per member.
- Enum Members can be added with one or two parameters.
When only one parameter is passed it is expcected to be a string. This single parameter will be used as doc string
When two parameters are pass the first parameter is expected to be an int. The second parameter is expected to be str.
First parameter is the value of the enum member and must not be less or equal the highest member value used thus far.
Second parameter is the doc string.
example: simple auto-number, w/o docstring
class MyAutoEnum(AutoEnum): FIRST = () SECOND = () THIRD = () FOURTH = () print(MyAutoEnum.FIRST.value) # 1 print(MyAutoEnum.SECOND.value) # 2 print(MyAutoEnum.THIRD.value) # 3 print(MyAutoEnum.FOURTH.value) # 4
example: simple doc string
import inspect class MyAutoEnum(AutoEnum): FIRST = 'First value' SECOND = 'Second value' THIRD = 'Third value' FOURTH = 'Fourth value' print(MyAutoEnum.FIRST.value) # 1 print(MyAutoEnum.THIRD.value) # 3 print(inspect.getdoc(MyAutoEnum.FIRST)) # First value print(MyAutoEnum.THIRD.__doc__) # Third value
example: One Param to set only auto-number next sequence
class OnlyAutoEnum(AutoEnum): NEG = -999 LESS_NEG = () NONE = 0 FIRST = () SECOND = () TEN = 10 TEN_PLUS = () TWENTY = 20 TWENTY_PLUS = () print(OnlyAutoEnum.NEG.value) # -999 print(OnlyAutoEnum.LESS_NEG.value) # -998 print(OnlyAutoEnum.NONE.value) # 0 print(OnlyAutoEnum.SECOND.value) # 2 print(OnlyAutoEnum.TWENTY.value) # 20 print(OnlyAutoEnum.TEN_PLUS.value) # 11 print(OnlyAutoEnum.TEN_PLUS > OnlyAutoEnum.TEN) # True
example: Two Parameters
class OthAutoEnum(AutoEnum): NONE = (0, 'Represents that no value is set yet') FIRST = 'First value' SECOND = 'Second value' TEN = (10, 'Represents 10') TEN_PLUS = 'Represents above 10' TWENTY = (20, 'Represents 20') TWENTY_PLUS = 'Represents above 20' print(OthAutoEnum.NONE.value) # 0 print(OthAutoEnum.SECOND.value) # 2 print(OthAutoEnum.TWENTY.value) # 20 print(OthAutoEnum.TEN_PLUS.value) # 11 print(OthAutoEnum.TEN_PLUS > OthAutoEnum.TEN) # True print(OthAutoEnum.TWENTY.__doc__) # Represents 20