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.4.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.4-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.4-cp312-cp312-macosx_15_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

saengra-0.1.4-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.4-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.4.tar.gz.

File metadata

  • Download URL: saengra-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 955d98ec5184687c182f2a962ed6531bf4d09b3e8f0f3dc0e320b547c773d16a
MD5 51e823c2f57330ee5f574a064a232f85
BLAKE2b-256 587bf247d257f74a2fdb01e02d368635cdebe693b2e3a4170222c480314568c1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ea1e59ac3d7949b6ca623e87bc439e5790e106c2aab83fa7da7e376a11c585f5
MD5 9885f895f90e7f26f36167885aa58472
BLAKE2b-256 bf70461d35c749f2901162f55930de6db506130e8eac91ac7208aec85dd26cf3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.4-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fd1a6573d718db6da7b5c76cc6d306b96076d5021c7b14ebd9684dace39f5d4c
MD5 900b3d51925c638a96d8f03e3f15082e
BLAKE2b-256 c3a1885517145fd843c7db1ee58f5ee31b242094a771739f81355ce959dc6152

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 14c0f3dc40a7d593047da84ca50863193a4592514d76b9f365db1bfbc6ad1698
MD5 e46b17ce5b17783b17044d7628ce7022
BLAKE2b-256 937dabac0d1207fbd750cb3063ebcefc22d56deab6765997b2149f3d6b19cb68

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.4-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 63d0275b051deb1753b8a8a42593000d87be2ef1634f22d0137ab5925c7c46cc
MD5 3ae231ff641c4005c9c74c8e4c473a7f
BLAKE2b-256 f663d7f27a51ab68b1fbf07c1a6cb3440eb2ae14f1a21920a7f9ad53780e826d

See more details on using hashes here.

Provenance

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