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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file saengra-0.1.11.tar.gz.
File metadata
- Download URL: saengra-0.1.11.tar.gz
- Upload date:
- Size: 87.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ceeb49ae39acd4b785e0535489d4e243c73da0f84d3008c1d7709cbf27dfea7
|
|
| MD5 |
f769dab22f1722d71676853f3269f688
|
|
| BLAKE2b-256 |
6ad301ea6d92c1c1f3cf69000b99b2aa75d75a6489415d554863d519f0c9f583
|
Provenance
The following attestation bundles were made for saengra-0.1.11.tar.gz:
Publisher:
build-wheels.yml on Saluev/saengra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
saengra-0.1.11.tar.gz -
Subject digest:
2ceeb49ae39acd4b785e0535489d4e243c73da0f84d3008c1d7709cbf27dfea7 - Sigstore transparency entry: 1155148418
- Sigstore integration time:
-
Permalink:
Saluev/saengra@cf144f522fb36228bff11db948cf11edf745d6d2 -
Branch / Tag:
refs/tags/v0.1.11 - Owner: https://github.com/Saluev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-wheels.yml@cf144f522fb36228bff11db948cf11edf745d6d2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file saengra-0.1.11-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: saengra-0.1.11-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 4.6 MB
- Tags: CPython 3.12, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
952cf9d5f1c8755314d69cb74bcfa010275932f765c6a1043346d9884e5ba204
|
|
| MD5 |
bbd163d82d59cb3e45ed8d31dcc6453f
|
|
| BLAKE2b-256 |
b3868fed4a6d12bf9d63e06746bf131018e5312c6e4e4268a6f66d0495c56cc0
|
Provenance
The following attestation bundles were made for saengra-0.1.11-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
build-wheels.yml on Saluev/saengra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
saengra-0.1.11-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
952cf9d5f1c8755314d69cb74bcfa010275932f765c6a1043346d9884e5ba204 - Sigstore transparency entry: 1155148422
- Sigstore integration time:
-
Permalink:
Saluev/saengra@cf144f522fb36228bff11db948cf11edf745d6d2 -
Branch / Tag:
refs/tags/v0.1.11 - Owner: https://github.com/Saluev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-wheels.yml@cf144f522fb36228bff11db948cf11edf745d6d2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file saengra-0.1.11-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: saengra-0.1.11-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.12, manylinux: glibc 2.27+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e19eef3c188d75a87494287e19c2de641e388e06497e0740b8c4fbd29c41d75
|
|
| MD5 |
27ce2ada2933bedaf04e62c2bd696650
|
|
| BLAKE2b-256 |
48951759fa40715ffc832191055a1993c5d06bd5f10d31850fcbf14de7848648
|
Provenance
The following attestation bundles were made for saengra-0.1.11-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:
Publisher:
build-wheels.yml on Saluev/saengra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
saengra-0.1.11-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl -
Subject digest:
3e19eef3c188d75a87494287e19c2de641e388e06497e0740b8c4fbd29c41d75 - Sigstore transparency entry: 1155148429
- Sigstore integration time:
-
Permalink:
Saluev/saengra@cf144f522fb36228bff11db948cf11edf745d6d2 -
Branch / Tag:
refs/tags/v0.1.11 - Owner: https://github.com/Saluev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-wheels.yml@cf144f522fb36228bff11db948cf11edf745d6d2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file saengra-0.1.11-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: saengra-0.1.11-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 4.6 MB
- Tags: CPython 3.11, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2d8db1fbca150ec2d12e228c2785b41d83eda7dec41f343179b150b6c748ef2
|
|
| MD5 |
1610e2df2175c6fc9dcaf128e7362782
|
|
| BLAKE2b-256 |
983e9f89566ce5cc809d5b0d9008bb157119b64f216056e3fe1a02c58a8ed02c
|
Provenance
The following attestation bundles were made for saengra-0.1.11-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
build-wheels.yml on Saluev/saengra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
saengra-0.1.11-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
f2d8db1fbca150ec2d12e228c2785b41d83eda7dec41f343179b150b6c748ef2 - Sigstore transparency entry: 1155148425
- Sigstore integration time:
-
Permalink:
Saluev/saengra@cf144f522fb36228bff11db948cf11edf745d6d2 -
Branch / Tag:
refs/tags/v0.1.11 - Owner: https://github.com/Saluev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-wheels.yml@cf144f522fb36228bff11db948cf11edf745d6d2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file saengra-0.1.11-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.
File metadata
- Download URL: saengra-0.1.11-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, manylinux: glibc 2.27+ ARM64, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
901b131d8972adb52364b397d1822a6c605f7ec49a14c26aa3ab8e7afc645fc3
|
|
| MD5 |
d561b9456adbdc94c91251901847328b
|
|
| BLAKE2b-256 |
407431254075be41558e254619d46cd30684d8f5b56fb280827cbdd7e26f8af2
|
Provenance
The following attestation bundles were made for saengra-0.1.11-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:
Publisher:
build-wheels.yml on Saluev/saengra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
saengra-0.1.11-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl -
Subject digest:
901b131d8972adb52364b397d1822a6c605f7ec49a14c26aa3ab8e7afc645fc3 - Sigstore transparency entry: 1155148419
- Sigstore integration time:
-
Permalink:
Saluev/saengra@cf144f522fb36228bff11db948cf11edf745d6d2 -
Branch / Tag:
refs/tags/v0.1.11 - Owner: https://github.com/Saluev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-wheels.yml@cf144f522fb36228bff11db948cf11edf745d6d2 -
Trigger Event:
push
-
Statement type: