How To Iterate Within A Specific Range Of Enum Class With Hex Value In Python
I have the following class enum: class LogLevel(Enum): level_1 = 0x30 level_2 = 0x31 level_3 = 0x32 level_4 = 0x33 level_5 = 0x34 level_6 = 0x35 level_7
Solution 1:
If levels sorted in ascending order then try:
list(LogLevel)[4:6]
which gives:
>> [<LogLevel.level_5:52>, <LogLevel.level_6:53>]
If levels are not sorted then try:
levels = ['level_5','level_6']
[i for i in list(LogLevel) if i.name in levels]
which gives
[<LogLevel.level_5:52>, <LogLevel.level_6:53>]
Solution 2:
>>>[LogLevel(i) for i inrange(LogLevel.level_5.value, LogLevel.level_7.value)]
[<LogLevel.level_5: 52>, <LogLevel.level_6: 53>]
You can simplify that by using IntEnum
:
>>>[LogLevel(i) for i inrange(LogLevel.level_5, LogLevel.level_7)]
[<LogLevel.level_5: 52>, <LogLevel.level_6: 53>]
Post a Comment for "How To Iterate Within A Specific Range Of Enum Class With Hex Value In Python"