Skip to main content

Helps you to build fancy statemachines

Project description

statez

Total alerts statez.png

Motivation

All the statemachine packages for python look weird and do too much stuff. This one is simple (and can even support asynchronous transitions).

Installation

pip install statez

# or if you use poetry
poetry add statez

Usage

Synchronous example

from statez import (
    Trigger,
    From,
    To,
    Do,
    StateMachine,
    Event
)

if __name__ == '__main__':
    s = StateMachine("HungryBoi", state="hungry")
    transition = Trigger("Eat") | From(["hungry", "dunno"]) | To("not_hungry") | Do(lambda a: True)
    # It doesn't matter if you use the function directly or if you wrap it in Do :-)
    assert transition == Trigger("Eat") | From(["hungry", "dunno"]) | To("not_hungry") | (lambda a: True)
    s += transition
    s.consume(Event("Eat"))
    assert s.state == "not_hungry", s.state

Asynchronous example (Caution, this is dumb use of asyncio)

from statez import (
    Trigger,
    From,
    To,
    Do,
    AsyncStateMachine,
    Event,
)
import asyncio


async def return_bool(ignore):
    return True


async def say_stuff(ignore):
    print("stuff")
    return True


if __name__ == "__main__":
    s = AsyncStateMachine("HungryBoi", state="hungry")
    transition = (
        Trigger("Eat") | From(["hungry", "dunno"]) | To("not_hungry") | Do(return_bool)
    )
    # This is how you can keep the state as before
    transition2 = Trigger("SayStuff") | From(...) | To(...) | say_stuff

    # It doesn't matter if you use the function directly or if you wrap it in Do :-)
    assert (
        transition
        == Trigger("Eat") | From(["hungry", "dunno"]) | To("not_hungry") | return_bool
    )
    s += transition
    s += transition2
    asyncio.run(s.consume(Event("Eat")))
    assert s.state == "not_hungry", s.state
    asyncio.run(s.consume(Event("SayStuff")))
    assert s.state == "not_hungry", s.state

License

This project is licensed under the GPL-3 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

statez-0.3.4.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

statez-0.3.4-py3-none-any.whl (15.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page