Kiwi Cogs
A simple and easy to use state machine library.
- Github repository: https://github.com/mopeyjellyfish/KiwiCogs/
- Documentation https://mopeyjellyfish.github.io/KiwiCogs/
Installation
Pip
pip install -U kiwi-cogs
Poetry
poetry add kiwi-cogs
Quick start
Events
Example configuration:
light_config = {
"name": "lights",
"initial": "green",
"states": {
"green": {
"events": {"NEXT": {"target": "yellow"}},
},
"yellow": {"events": {"NEXT": {"target": "red"}}},
"red": {"events": {"NEXT": {"target": "green"}}},
},
}
Usage:
light_machine await Machine.create(light_config)
assert traffic_light.initial_state.value == "green"
yellow_state = await traffic_light.event("NEXT")
assert yellow_state.value == "yellow"
red_state = await traffic_light.event("NEXT")
assert red_state.value == "red"
green_state = await traffic_light.event("NEXT")
assert green_state.value == "green"
Transitions
Example configuration:
async def entered(_):
print("entered state!")
async def log(_):
print("LOG!")
def exited(_):
print("exited!")
def is_adult(context, _):
age = context.get("age")
return age is not None and age >= 18
def is_child(context, _):
age = context.get("age")
return age is not None and age < 18
def log_age(context):
age = context.get("age")
print(f"User is {age} old!")
def age_determined(context):
age = context.get("age")
print(f"Users age has been determined as: {age}")
age_config = {
"name": "age",
"context": {"age": None}, # age unknown
"initial": "unknown",
"states": {
"unknown": {
"transitions": [
{"target": "adult", "cond": is_adult},
{"target": "child", "cond": is_child},
],
"entry": [log, entered],
"exit": age_determined,
},
"adult": {"type": "final", "entry": log_age},
"child": {"type": "final", "entry": log_age},
},
}
Usage:
age_machine await Machine.create(age_config)
assert age_machine.state.value == "unknown"
context = {"age": 18}
await age_machine.with_context(context=context)
assert age_machine.state.value == "adult"
Hierarchical machine
Example configuration:
def is_walking(context, _):
return context["speed"] <= 11
def is_running(context, _):
return context["speed"] > 11
walk_states = {
"initial": "start",
"states": {
"start": {
"transitions": [ # resolved in order
{"target": "walking", "cond": is_walking},
{"target": "running", "cond": is_running},
],
},
"walking": {"events": {"CROSSED": {"target": "crossed"}}},
"running": {"events": {"CROSSED": {"target": "crossed"}}},
"crossed": {},
},
}
pedestrian_states = {
"initial": "walk",
"states": {
"walk": {"events": {"PED_COUNTDOWN": {"target": "wait"}}, **walk_states},
"wait": {"events": {"PED_COUNTDOWN": {"target": "stop"}}},
"stop": {},
"blinking": {},
},
}
crossing_config = {
"name": "light",
"initial": "green",
"context": {"speed": 10},
"states": {
"green": {"events": {"TIMER": {"target": "yellow"}}},
"yellow": {"events": {"TIMER": {"target": "red"}}},
"red": {"events": {"TIMER": {"target": "green"}}, **pedestrian_states},
},
"events": {
"POWER_OUTAGE": {"target": ".red.blinking"},
"POWER_RESTORED": {"target": ".red"},
},
}
Example usage:
crossing = await Machine.create(crossing_config)
assert crossing.initial_state.value == "green"
assert crossing.state.type == "atomic"
await crossing.event("TIMER")
assert crossing.state.value == "yellow"
assert crossing.state.type == "atomic"
await crossing.event("TIMER")
assert crossing.state.value == {"red": {"walk": "walking"}}
await crossing.event("CROSSED")
assert crossing.state.value == {"red": {"walk": "crossed"}}
assert crossing.state.type == "compound"
await crossing.event("PED_COUNTDOWN")
assert crossing.state.value == {"red": "wait"}
await crossing.event("PED_COUNTDOWN")
assert crossing.state.value == {"red": "stop"}
await crossing.event("TIMER")
assert crossing.initial_state.value == "green"
assert crossing.state.type == "atomic"
Metadata
Release files for kiwi-cogs 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kiwi_cogs-0.1.2.tar.gz | 10.5 kB | Without provenance | Details for kiwi_cogs-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kiwi_cogs-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for kiwi_cogs-0.1.2-py3-none-any.whl |
Total release size: 21.9 kB
Release files / kiwi_cogs-0.1.2.tar.gz
| Download URL | kiwi_cogs-0.1.2.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00fdccf58aa388c7b36076085990aa2b1ba2ec60e0f52c20f6a8fc961fe30ae2
|
|
BLAKE2b-256 checksum How to use checksums |
b19074561a5531f83cf1f47933ba87c3cdb293eb0fe52ca704bd406e4efb7107
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.11.3 Linux/5.15.0-1038-azure
|
Release files / kiwi_cogs-0.1.2-py3-none-any.whl
| Download URL | kiwi_cogs-0.1.2-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d3458a78f0f6f142b1bebd5fd7377d720969ec5679e2cb6eaa45f4f3ce1dfaaf
|
|
BLAKE2b-256 checksum How to use checksums |
fc933cd610b10bbeeb2216c6d6837b20fb26bec39e4121ac05faba433f6775b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.11.3 Linux/5.15.0-1038-azure
|
Security for kiwi-cogs 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.