Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python-statemachine: Python Finite State Machines made easy. #487

Open
1 task
irthomasthomas opened this issue Jan 31, 2024 · 0 comments
Open
1 task

python-statemachine: Python Finite State Machines made easy. #487

irthomasthomas opened this issue Jan 31, 2024 · 0 comments
Labels
New-Label Choose this option if the existing labels are insufficient to describe the content accurately python Python code, tools, info software-engineering Best practice for software engineering source-code Code snippets TIL Short notes or tips on coding, linux, llms, ml, etc

Comments

@irthomasthomas
Copy link
Owner

fgmacedo/python-statemachine: Python Finite State Machines made easy.

Python finite-state machines made easy.

Python StateMachine

Welcome to python-statemachine, an intuitive and powerful state machine framework designed for a great developer experience.

🚀 With StateMachine, you can easily create complex, dynamic systems with clean, readable code.

💡 Our framework makes it easy to understand and reason about the different states, events and transitions in your system, so you can focus on building great products.

🔒 python-statemachine also provides robust error handling and ensures that your system stays in a valid state at all times.

A few reasons why you may consider using it:

📈 python-statemachine is designed to help you build scalable, maintainable systems that can handle any complexity.
💪 You can easily create and manage multiple state machines within a single application.
🚫 Prevents common mistakes and ensures that your system stays in a valid state at all times.

Getting started

To install Python State Machine, run this command in your terminal:

pip install python-statemachine

To generate diagrams from your machines, you'll also need pydot and Graphviz. You can install this library already with pydot dependency using the extras install option. See our docs for more details.

pip install python-statemachine[diagrams]

Define your state machine:

from statemachine import StateMachine, State

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

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

    def before_cycle(self, event: str, source: State, target: State, message: str = ""):
        message = ". " + message if message else ""
        return f"Running {event} from {source.id} to {target.id}{message}"

    def on_enter_red(self):
        print("Don't move.")

    def on_exit_red(self):
        print("Go ahead!")

You can now create an instance:

sm = TrafficLightMachine()

This state machine can be represented graphically as follows:

img_path = "docs/images/readme_trafficlightmachine.png"
sm._graph().write_png(img_path)

URL

Suggested labels

{ "label-name": "state-machine-framework", "description": "A framework for creating complex, dynamic systems with clean, readable code.", "confidence": 85.89 }

@irthomasthomas irthomasthomas added New-Label Choose this option if the existing labels are insufficient to describe the content accurately python Python code, tools, info source-code Code snippets TIL Short notes or tips on coding, linux, llms, ml, etc labels Jan 31, 2024
@irthomasthomas irthomasthomas changed the title fgmacedo/python-statemachine: Python Finite State Machines made easy. python-statemachine: Python Finite State Machines made easy. Mar 16, 2024
@irthomasthomas irthomasthomas added the software-engineering Best practice for software engineering label Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New-Label Choose this option if the existing labels are insufficient to describe the content accurately python Python code, tools, info software-engineering Best practice for software engineering source-code Code snippets TIL Short notes or tips on coding, linux, llms, ml, etc
Projects
None yet
Development

No branches or pull requests

1 participant