[docs]classTransitionList(AddCallbacksMixin):"""A list-like container of :ref:`transitions` with callback functions."""
[docs]def__init__(self,transitions:"Iterable[Transition] | None"=None):""" Args: transitions: An iterable of `Transition` objects. Defaults to `None`. """self.transitions:List[Transition]=list(transitions)iftransitionselse[]
[docs]def__repr__(self):"""Return a string representation of the :ref:`TransitionList`."""returnf"{type(self).__name__}({self.transitions!r})"
[docs]def__or__(self,other:"TransitionList | Iterable"):"""Return a new :ref:`TransitionList` that combines the transitions of this :ref:`TransitionList` with another :ref:`TransitionList` or iterable. Args: other: Another :ref:`TransitionList` or iterable of :ref:`Transition` objects. Returns: TransitionList: A new :ref:`TransitionList` object that combines the transitions of this :ref:`TransitionList` with `other`. """returnTransitionList(self.transitions).add_transitions(other)
[docs]defadd_transitions(self,transition:"Transition | TransitionList | Iterable"):"""Adds one or more transitions to the :ref:`TransitionList` instance. Args: transition: A sequence of transitions or a :ref:`TransitionList` instance. Returns: The updated :ref:`TransitionList` instance. """ifisinstance(transition,TransitionList):transition=transition.transitionstransitions=ensure_iterable(transition)fortransitionintransitions:assertisinstance(transition,Transition)# makes mypy happyself.transitions.append(transition)returnself
[docs]def__getitem__(self,index:int)->"Transition":"""Returns the :ref:`transition` at the specified ``index``. Args: index: The index of the transition. Returns: The :ref:`transition` at the specified index. """returnself.transitions[index]
[docs]def__len__(self):"""Returns the number of transitions in the :ref:`TransitionList` instance. Returns: The number of transitions. """returnlen(self.transitions)
[docs]defadd_event(self,event:str):""" Adds an event to all transitions in the :ref:`TransitionList` instance. Args: event: The name of the event to be added. """fortransitioninself.transitions:transition.add_event(event)
@propertydefunique_events(self)->List["Event"]:""" Returns a list of unique event names across all transitions in the :ref:`TransitionList` instance. Returns: A list of unique event names. """tmp_ordered_unique_events_as_keys_on_dict={}fortransitioninself.transitions:foreventintransition.events:tmp_ordered_unique_events_as_keys_on_dict[event]=Truereturnlist(tmp_ordered_unique_events_as_keys_on_dict.keys())