Skip to content

Pure Python implementation of a state machine that can be initialized with the transitions it can take and an initial state.

License

Notifications You must be signed in to change notification settings

ghandic/StateMachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python State Machine

made-with-python made-with-Markdown MIT license Open Source? Yes! coverage

Pure Python implementation of a state machine that can be initialized with the transitions it can take and an initial state. It can also add convenience methods to your class based on the states you have defined such as is_stopped for a state of "stopped".

Concepts covered

  • Object Oriented Programming
  • Decorators
  • Introspection
  • Unit testing
  • Error handling
  • Advanced control flow
  • Advanced Python syntax

Example usage

from machine import Machine

class Video(object):
    PAUSED = "paused"
    PLAYING = "playing"
    STOPPED = "stopped"

    def __init__(self, skip_optional_validation=True):
        self.skip_optional_validation = skip_optional_validation
        transitions = [
            {"trigger": "play", "source": self.PAUSED, "dest": self.PLAYING},
            {"trigger": "play", "source": self.STOPPED, "dest": self.PLAYING},
            {"trigger": "pause", "source": self.PLAYING, "dest": self.PAUSED},
            {"trigger": "stop", "source": self.PLAYING, "dest": self.STOPPED},
            {"trigger": "stop", "source": self.PAUSED, "dest": self.STOPPED},
        ]

        self.machine = Machine(self, transitions, self.STOPPED, skip_optional_validation=True)

    def play(self):
        ...

    def pause(self):
        ...

    def stop(self):
        ...

Now when you call play(), the machine's state will be updated to "playing", if you run pause(), the machine's state will be updated to "stopped", if you try to stop it again in this state it will raise a MachineError as you cannot transition from stopped to stopped.

License

This project is licensed under the terms of the MIT license.

About

Pure Python implementation of a state machine that can be initialized with the transitions it can take and an initial state.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published