
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/traffic_light_machine.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_traffic_light_machine.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_traffic_light_machine.py:


Traffic light machine
---------------------

Demonstrates the concept of ``cycle`` states.

.. GENERATED FROM PYTHON SOURCE LINES 8-26



.. image-sg:: /auto_examples/images/sphx_glr_traffic_light_machine_001.svg
   :alt: traffic light machine
   :srcset: /auto_examples/images/sphx_glr_traffic_light_machine_001.svg
   :class: sphx-glr-single-img





.. code-block:: default

    from statemachine import State
    from statemachine import StateMachine


    class TrafficLightMachine(StateMachine):
        "A traffic light machine"
        green = State("Green", initial=True)
        yellow = State("Yellow")
        red = State("Red")

        cycle = green.to(yellow) | yellow.to(red) | red.to(green)

        def on_cycle(self, event_data=None):
            return "Running {} from {} to {}".format(
                event_data.event,
                event_data.source.id,
                event_data.target.id,
            )


.. _sphx_glr_download_auto_examples_traffic_light_machine.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example


    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: traffic_light_machine.py <traffic_light_machine.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: traffic_light_machine.ipynb <traffic_light_machine.ipynb>`
