timed_phase_enum.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. from enum import Enum
  2. class TimedPhaseEnum(Enum):
  3. """ Enum that can represent states, cycled through by calling the next() method"""
  4. def __new__(cls, identifier, timing: float = 0.0):
  5. # Create a new instance with a unique identifier as its value
  6. obj = object.__new__(cls)
  7. obj._value_ = identifier # The unique value
  8. obj.timing = timing # Additional attribute for timing
  9. return obj
  10. def next(self):
  11. members = list(self.__class__)
  12. index = members.index(self)
  13. return members[(index + 1) % len(members)]
  14. if __name__ == '__main__':
  15. class MyEnum(TimedPhaseEnum):
  16. IDLE = ("IDLE", float('inf'))
  17. STEP_1 = ("Step 1", 1.0)
  18. STEP_2 = ("Step 2", 5.0)
  19. STEP_3 = ("Step 3", 4.0)
  20. STEP_4 = ("Step 4", 5.0)
  21. STEP_5 = ("Step 5", 5.0)
  22. DONE = ("DONE", 0.0)
  23. curr_step = MyEnum.IDLE
  24. for _ in range(len(MyEnum)):
  25. print(f"{curr_step} (timing = {curr_step.timing})")
  26. curr_step = curr_step.next()