Skip to main content

Add your description here

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
  • ReducersExtendList, 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.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

justgraph-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file justgraph-0.1.0.tar.gz.

File metadata

  • Download URL: justgraph-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 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

Hashes for justgraph-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b63d46c18f8db8e856a5529974643bcb49919a73ae61e5d0fcf37c926a366a2
MD5 147d99a5c5666d8b447e02258f7a1d35
BLAKE2b-256 dd72b698cccf4aca352d9c11bed93ee1e2055c057675ca119e8775124a0ed790

See more details on using hashes here.

File details

Details for the file justgraph-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: justgraph-0.1.0-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

Hashes for justgraph-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc9e6e1f83844aa8842f311604bd182c129bc2a3b78692c20241fc88e7b9b53c
MD5 342353718c3491e7543b5486b37a509e
BLAKE2b-256 367c8302bec7e4e6f48084129736158b6558f5125b18e1aebc3ec39fe913e147

See more details on using hashes here.

Supported by

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