0

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 = 0x36
    level_8 = 0x37
    level_9 = 0x38

I need to iterate from level_5 to level_6.

I tried islice() but haven't had any success

1

2 Answers 2

2

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>]
Sign up to request clarification or add additional context in comments.

Comments

1
>>> [LogLevel(i) for i in range(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 in range(LogLevel.level_5, LogLevel.level_7)]
[<LogLevel.level_5: 52>, <LogLevel.level_6: 53>]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.