Skip to main content

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.

Metadata

Release files for statez 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for statez 0.3.4
File Size Uploaded
statez-0.3.4.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for statez 0.3.4
File Interpreter ABI Platform
statez-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / statez-0.3.4.tar.gz

Download URL statez-0.3.4.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fccaccce5108e5559cce024135d098c3779366e727b129b264205f30260c05ee
BLAKE2b-256 checksum
How to use checksums
4ec6940c6f8ca40d14ea811ebe4f505d90e863d2bf8a9159ffc36de033217408
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-37-generic

Release files / statez-0.3.4-py3-none-any.whl

Download URL statez-0.3.4-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e07557ae2b10aa5f59063af1f7f466360bbbba2e53599b49961382ff8c6e4cc
BLAKE2b-256 checksum
How to use checksums
9acb3bf96d31678aec8f6e22706d5b64a618f1e6e550f489f53e370a62e6886e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-37-generic

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page