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.6.tar.gz (85.2 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.6-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.6-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.6-cp312-cp312-macosx_15_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

saengra-0.1.6-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.6-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.6-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.6.tar.gz.

File metadata

  • Download URL: saengra-0.1.6.tar.gz
  • Upload date:
  • Size: 85.2 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.6.tar.gz
Algorithm Hash digest
SHA256 bde8284175d8bc12ecd7de4d0d6daafec0686174a308048d6094b71b05167772
MD5 5cbb0d374e4421ad2fda74448c7d8de5
BLAKE2b-256 09facdd51dd6160a0764fb738b9f4ea23d9d0c8735dce7c21fc510b82558963e

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6.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.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for saengra-0.1.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 67b90c172ef21c0422621b0d1bc916ac4d10d290b67046bce15fe6821688511f
MD5 7d9d3ae7bce7b808f98a6c40bd495f65
BLAKE2b-256 1dc2df7b91fd5b7315bd84cc22347a297039f13d010933d0087391a98021c3c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6-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.6-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for saengra-0.1.6-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7d0382d9439826c086ed1b2fab4c4dc2b4eb7f7576d9086f26202c3b8e766190
MD5 faf68e6f7453d4d61754318faf0fce45
BLAKE2b-256 1f43a158190d64768fd911e132de14674f81b881273d1e3aaf484fde7768fc61

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6-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.6-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for saengra-0.1.6-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0818cc84799a1f251a4246ffd408fbea7efe84d57b6a7802f58913a5b0027a15
MD5 1a03923450ac0c7421e8c13587189142
BLAKE2b-256 d81386e51f0b5cd4421ead757900890986352314e9f12d4a4abc5da9e0696623

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6-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.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for saengra-0.1.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a7e375f0658a998a6e667842b267dd2d1192661d05fd4f858b5ac4406f5acbdd
MD5 9e28d343e9e06a3ccb7ef2bbf57285b5
BLAKE2b-256 6e7431e1f325295f7cedd8b31a5aa98e5697bb1579393adabbae3fe91b2ac0fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6-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.6-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for saengra-0.1.6-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bdbe0d4e53823a38c8876aa05255643997bca40a62a1e792181db501e83c271b
MD5 6999c1e3c4448344201ff8703de75642
BLAKE2b-256 04585d27a3c4e70d772560afad19660b06893f6bbb72dca9e92a44c061c67003

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6-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.6-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for saengra-0.1.6-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fe76015a2c04099e1fb883fbae735d96f489259b3849ad04737cef0c191795f2
MD5 bc67ce94b61ddf36b6911cbcb351b1fd
BLAKE2b-256 df3d837cf998c7d135100b206e8bdd65ba0a7331f0bbb791637dbbb7f8c0fc32

See more details on using hashes here.

Provenance

The following attestation bundles were made for saengra-0.1.6-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