123456789101112131415161718192021222324252627282930313233 |
- from enum import Enum
- class TimedPhaseEnum(Enum):
- """ Enum that can represent states, cycled through by calling the next() method"""
- def __new__(cls, identifier, timing: float = 0.0):
- # Create a new instance with a unique identifier as its value
- obj = object.__new__(cls)
- obj._value_ = identifier # The unique value
- obj.timing = timing # Additional attribute for timing
- return obj
- def next(self):
- members = list(self.__class__)
- index = members.index(self)
- return members[(index + 1) % len(members)]
- if __name__ == '__main__':
- class MyEnum(TimedPhaseEnum):
- IDLE = ("IDLE", float('inf'))
- STEP_1 = ("Step 1", 1.0)
- STEP_2 = ("Step 2", 5.0)
- STEP_3 = ("Step 3", 4.0)
- STEP_4 = ("Step 4", 5.0)
- STEP_5 = ("Step 5", 5.0)
- DONE = ("DONE", 0.0)
- curr_step = MyEnum.IDLE
- for _ in range(len(MyEnum)):
- print(f"{curr_step} (timing = {curr_step.timing})")
- curr_step = curr_step.next()
|