Efficient graph database on disk
Project description
A graph database in a single file
Kinbaku is the japanese art of bondage. In a graph, ropes are replaced by edges between nodes. This library allows you to manage large graphs on disk, using only one file and without having to load the whole graph in memory. The library is written in pure Python.
- Source: https://github.com/kerighan/kinbaku
- Documentation: https://kinbaku.readthedocs.io
- Tutorial: https://kinbaku.readthedocs.io/en/latest/tutorial.html
Installation
It is recommended to install the cityhash package:
pip install cityhash
To install Kinbaku:
pip install kinbaku
Basic usage
import kinbaku as kn
# create graph if the file doesn't already exist
G = kn.Graph("graph.db")
# add nodes
G.add_node("A") # keys must be strings
G.add_node("B")
G.add_node("C")
# add edges
G.add_edge("A", "B")
G.add_edge("A", "C")
# get a node
print(G.node("A"))
print(G["A"])
# get out neighbors
print(list(G.neighbors("A")))
# get incoming nodes
print(list(G.predecessors("B")))
# iterating through the nodes
for node in G.nodes:
print(node)
# iterating through the edges
for edge in G.edges:
print(edge)
Node keys must imperatively be strings with a maximum length. The maximum length can be set before the graph is created using the max_key_len keyword argument.
Using custom attributes
With Kinbaku, nodes and edges can have custom attributes. The way to proceed is to create Python dataclasses that inherit from Kinbaku structures.
from dataclasses import dataclass
import kinbaku as kn
@dataclass
class User(kn.structure.Node):
age: int = 0
bio: str = ""
@dataclass
class Relation(kn.structure.Edge):
weight: float = 0
G = kn.Graph("graph_with_attributes.db",
node_class=User,
edge_class=Relation,
max_str_len=40) # max string length
# using the 'add_node' method:
G.add_node("Mark", {"age": 25, "bio": "first text"})
# or using '__setitem__':
G["Mary"] = {"age": 32, "bio": "second text"}
# adding an edge with custom attributes:
G.add_edge("Mark", "Mary", {"weight": .1})
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 Distribution
File details
Details for the file kinbaku-0.0.2.tar.gz
.
File metadata
- Download URL: kinbaku-0.0.2.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ebf264449b57ebdc567f4d4622d0080bf8ec044afcc78958aef92761fb16cff |
|
MD5 | 1fb15686b925827bd4be9d3bb161c781 |
|
BLAKE2b-256 | 481f40898c511e87230c6d4bd10c6f40ecbbbca3e75c2800444f3991f094349b |
File details
Details for the file kinbaku-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: kinbaku-0.0.2-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d47a1f68541ed3d37d2435517fd93f8247068835a376f2c5c8c44baf4d9ac2d2 |
|
MD5 | c97fd8f0b1c076381eb1d1626b19836a |
|
BLAKE2b-256 | ed5766f3d00609e24f7b7e0ac6af532915fad20b32578675eaa09a0e9712f5fb |