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

Uploaded CPython 3.12macOS 15.0+ ARM64

saengra-0.1.5-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.5-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.5.tar.gz.

File metadata

  • Download URL: saengra-0.1.5.tar.gz
  • Upload date:
  • Size: 85.1 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.5.tar.gz
Algorithm Hash digest
SHA256 9f8223b6232e0bfa45be49b56c8256b24581e6e2cd393ca8db2eda1a116b9394
MD5 5511165da61a8702b1989b2dc4d01177
BLAKE2b-256 35f22d41ac2098c90d387bdc2d7a48248565e52996869c791670c22e1ffe0587

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.5-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dc121004256f80f9fa6a18e3d1c848cd0ad4234bcc3c468106a9953bc27298bf
MD5 8ab6436e33cfbd807f5c62e6b6ddcac9
BLAKE2b-256 a272ebf1522c9a55cbcfde2708d04e62e697482d2096698780690e8fd780531a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.5-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 8849a8196db20a5a4f051bfaac5c02ed3d807c70ec8c307e97392e7a0e825ab3
MD5 fe66359e8833527e44cfda8e142c2a4d
BLAKE2b-256 8bc2a9d22b4290c8ade94aabca45ae6047810447bc6ecac4342338cde9602398

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fa3b8c8d1b92718c9c62f18addeb8258007ab07f34d59efc0421f2ef5c40baba
MD5 0c83260e6131faa7bc6670874cfdff2e
BLAKE2b-256 62f25bf55a2dc65b8a2fc99ea916f26db9987b532e7d68419959dcbebad09010

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for saengra-0.1.5-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 53fe0220b66755654a1cbad4770dc95aa4caeca46b03b966c579ce56d2d2b40f
MD5 8c8a15026286ee740c5cb08f5ddeb8ec
BLAKE2b-256 d27f860a5091093834ac6507cbbcf879d6017b91a17104b20b16872016658d56

See more details on using hashes here.

Provenance

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