A lightweight graph-based state machine.
Project description
justgraph
A lightweight graph-based state machine.
from dataclasses import dataclass
from justgraph import State, FieldUpdate, Graph
from justgraph.reducers import ExtendList, Increment
@dataclass
class ChatState(State):
messages: list[str]
counter: int
graph = Graph([ChatState])
@graph.node("greet")
def greet(state: ChatState) -> list[FieldUpdate]:
return [
FieldUpdate(ChatState, "messages", ExtendList(["hello"])),
FieldUpdate(ChatState, "counter", Increment(1)),
]
@graph.node("log")
def log(state: ChatState) -> list[FieldUpdate]:
print(state.messages)
return []
graph.set_entry_point("greet").add_edge("greet", "log")
app = graph.compile()
app.invoke([ChatState(messages=[], counter=0)])
# ['hello']
Custom Reducers
Subclass Reducer[T] and implement apply(old: T) -> T:
from justgraph import Reducer, FieldUpdate
class Multiply(Reducer[int]):
def __init__(self, factor: int):
self._factor = factor
def apply(self, old: int) -> int:
return old * self._factor
# Use it like any built-in reducer
FieldUpdate(ChatState, "counter", Multiply(3))
Features
- Nodes — functions that receive state and return
FieldUpdates - Edges — wire nodes into a directed graph
- Conditional edges — route based on state
- Parallel fan-out — branches run concurrently via
ThreadPoolExecutor - Reducers —
ExtendList,Increment,Replace, or custom - Multiple states — nodes can depend on different state types
Examples
uv run examples/chat.py
uv run examples/fan_out.py
uv run examples/conditional.py
Tests
uv run pytest
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
justgraph-0.1.3.tar.gz
(4.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file justgraph-0.1.3.tar.gz.
File metadata
- Download URL: justgraph-0.1.3.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17187021e7154f552d13849da9e6bd9fb068d9c4b1e332323d046c3a76a2a6ea
|
|
| MD5 |
812fe7772f62b839e93a9c6b58cd7aa6
|
|
| BLAKE2b-256 |
61f8bc82b9f3100510e582e7049c3198f1d04b662d0de7eedd73e29b82e1d902
|
File details
Details for the file justgraph-0.1.3-py3-none-any.whl.
File metadata
- Download URL: justgraph-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e119599d83bf51684ba9f929a0001d0a19c8fe3b06ad37c8b681e59e18a6b553
|
|
| MD5 |
11cad2057c31f4a06a4cce27fc3903f5
|
|
| BLAKE2b-256 |
db8a3b07083524391b66f172fe900d461b5d3f8e25e05b4fef44d2a713f4364a
|