Skip to main content

Reactive graph database with pattern matching

Project description

Saengra

Python wrapper for Saengra graph database.

Quickstart: primitives and edges

Saengra is a graph database. It supports hashable Python objects (primitives) as graph vertices. Built-in types like int or str can be used directly; also Saengra provides @primitive decorator to declare dataclass-like types to be used as graph vertices.

Directed edges between primitives are always labelled with a string (edge label). It can be an arbitrary string, but the engine is optimized to support limited number of different labels per graph. There can't be two edges between the same two primitives with the same label.

We can construct a graph directly from primitives and edges by using elementary graph operations:

from datetime import datetime
from saengra import primitive, Environment
from saengra.graph import AddVertex, AddEdge

@primitive
class user:
    id: int

u1 = user(id=1)
u2 = user(id=2)
u1_registered_at = datetime(2022, 1, 1, 12, 0, 0)
u2_registered_at = datetime(2023, 2, 3, 15, 0, 0)

env = Environment()
env.update(
    AddVertex(u1),
    AddVertex(u2),
    AddVertex(u1_registered_at),
    AddVertex(u2_registered_at),
    AddEdge(u1, "follows", u2),
    AddEdge(u1, "registered_at", u1_registered_at),
    AddEdge(u2, "registered_at", u2_registered_at),
)
env.commit()

Quickstart: entities and environment

Operating with vertices and edges is tedious and slow. A higher-level abstraction, entities, is provided to make working with graph more like your normal object-oriented programming.

Let's declare some entity classes and rewrite the code above:

from datetime import datetime
from saengra import primitive, Entity, Environment

@primitive
class user:
    id: int

class User(Entity, user):
    registered_at: datetime
    follows: set["User"]

env = Environment(entity_types=[User])

u1 = User.create(env, id=1, registered_at=datetime(2022, 1, 1, 12, 0, 0))
u2 = User.create(env, id=2, registered_at=datetime(2023, 2, 3, 15, 0, 0))
u1.follows.add(u2)

env.commit()

Quickstart: expressions and observers

Saengra introduces a domain-specific language to describe subgraphs of the graph, i.e. subset of vertices and edges. These expressions are quite similar to queries in SQL.

# Find all subscriptions, i.e. pairs (u1, u2) where u1 follows u2:
env.match("user as u1 -follows> user as u2")
# -> [{"u1": User(id=1), "u2": User(id=2)}]

# Find all mutual subscriptions:
env.match("user as u1 <follows> user as u2")
# -> []

But the most powerful aspect of Saengra is its observation capability. Saengra can match expressions incrementally after processing graph updates, and notify the program about created, changed and deleted subgraphs after each commit.

from saengra import observer

mutual_follow = observer("user as u1 <follows> user as u2")


@mutual_follow.on_create
def notify_mutuals(u1: User, u2: User):
    print(f"{u1} is now mutuals with {u2}!")


env.register_observers([mutual_follow])

u2.follows.add(u1)
env.commit()
# -> User(id=1) is now mutuals with User(id=2)!
# -> User(id=2) is now mutuals with User(id=1)!

Generating Protobuf Code

The messages_pb2.py file is generated from the protobuf definitions in saengra-server/proto/messages.proto.

To regenerate:

protoc --python_out=saengra --proto_path=saengra-server/proto saengra-server/proto/messages.proto

Requirements:

  • protoc (Protocol Buffers compiler) must be installed
  • Python protobuf library: pip install protobuf>=4.21.0

Usage

Option 1: Automatically start server

from saengra.client import SaengraClient

# Client automatically starts saengra-server in background
with SaengraClient() as client:
    # Connect to a graph
    created = client.connect("my_graph")

    # Add vertices and edges
    client.apply_updates([
        # Your updates here
    ])

    # Commit changes
    response = client.commit()

The client expects saengra-server binary to be available in PATH.

Option 2: Connect to existing server

from saengra.client import SaengraClient

# Connect to an existing server socket
with SaengraClient(socket_path="/path/to/server.sock") as client:
    # Connect to a graph
    created = client.connect("my_graph")

    # Work with the graph...

When using an existing socket, the client will not start or stop the server process, and will not clean up the socket file.

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

saengra-0.1.7.tar.gz (85.4 kB view details)

Uploaded Source

Built Distributions

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

saengra-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

saengra-0.1.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

saengra-0.1.7-cp312-cp312-macosx_15_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

saengra-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

saengra-0.1.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

saengra-0.1.7-cp311-cp311-macosx_15_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

File details

Details for the file saengra-0.1.7.tar.gz.

File metadata

  • Download URL: saengra-0.1.7.tar.gz
  • Upload date:
  • Size: 85.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for saengra-0.1.7.tar.gz
Algorithm Hash digest
SHA256 d618d0ea1e594b81b493f723e72366a68a5f38003ce4cbd466158856e99af26e
MD5 143a052cc24f8371af788d6999fd1425
BLAKE2b-256 f445d6b55e6d5e30f48abe3e4c3e6960009e6b8b66bfd625f0428a2bb90ac136

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7.tar.gz:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file saengra-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for saengra-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d448b9718304a61174481932f544492623fd65dac38f13217657c1687910257f
MD5 21d933743741ae1ae54b87a42c4f21a9
BLAKE2b-256 96466d45c3ae950f1c6e5351ee56b1c2c21811b2761df2f7230c53fc5ac485fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file saengra-0.1.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for saengra-0.1.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 71ba7b55d7b28f69be2d79398b648951df22a8b21815d2fbb9ae8ddaa2c44803
MD5 db8c35c3792b217ba71db7f0a9e77754
BLAKE2b-256 c1a3cf74c280c8f9f1741356c9c132e369344830bfa8b2b17f90406c605eecd2

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file saengra-0.1.7-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for saengra-0.1.7-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ad9fe3606a635eb7fddd5d450cf021cb0b2ec986eba3717a3beebe0aa22bf227
MD5 a71e2255af398f77088c893d9b1912ac
BLAKE2b-256 0686f7e533b6c8a73a2f52c99757585c67e152eee0aa150822324130d341ff80

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file saengra-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for saengra-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 17637c38ea1cb521c4f2307cc3bc61d9dc683df8f6d483e538e30b0625802153
MD5 c24b19cd201e5d50c4f016ff9af07952
BLAKE2b-256 f6686a48244861eca2b252dc3840447ee2cec60324645414a38b5a1884cca32b

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file saengra-0.1.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for saengra-0.1.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e84c067016cf03579ab6211b12737949e866b3a507fce46fe625f9e31ea8f01b
MD5 70532131b17d4447a543acbc2b71fe3f
BLAKE2b-256 03b64b6702c8e00b602f835be637b6569659831912aa7b65f404061ee031361d

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file saengra-0.1.7-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for saengra-0.1.7-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f7d370b5a7d3ed7f09931c1deb7895e6fbc2ab090b221c89d99581f6bc37bf40
MD5 6a21e2b2eef1461be6102c49e146e18d
BLAKE2b-256 d919a1e523ff9ee98bad7b4b5984f0bd6af37c7c8a8a4105c16d785759bb62bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.7-cp311-cp311-macosx_15_0_arm64.whl:

Publisher: build-wheels.yml on Saluev/saengra

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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