What I need
I am writing a program where I want to define enum with possible interpretation of certain value that is stored one byte. So it is more specific than IntEnum as my value must be int in range 0x00-0xFF.
There are two types of values in the enum:
- specified by the standard (those I define on my own e.g. 0x00-0x7F)
- user specific (I let the User to define those, e.g. 0x80-0xFF)
Therefore, I am using aenum instead of classic enum package as my enum might register new members after the program is started.
What is desired solution
I would like to add some code to IntEnum to restrict possible member values, so it has to be both int and 0x00 <= value <= 0xFF or some exception would be raised.
Why? To stop user from defining improper values (e.g. -1, 256, 10.0) that are not int or not in range 0x00-0xFF.
What are my ideas
I thought to update __new__ method of IntEnum, but its nearly 500 lines long and it is rather hard to understand that. Though, I have notice it has boundary parameter. Isn't what I need (lower boundar = 0x00, higher bondary = 0xFF)?
My code looks like this now
from aenum import IntEnum, unique
@unique
class SomeByteEnum(IntEnum):
FOO = 1
BAR = 2
...
After the update, following cases shall raise an exception:
@unique
class SomeByteEnum(IntEnum):
... # some magical code here
INVALID_VALUE = -1 # value is lower than minimal byte value 0x00, exception shall be raised
from aenum import extend_enum
extend_enum(SomeByteEnum, "INVALID_VALUE", 0x100) # value is greater than max byte value 0xFF, exception shall be raised