Skip to main content

RDFLib plugin providing RDF/CBOR serialization

This is an implementation of the RDF/CBOR serialization for the Python RDFlib library.

RDF/CBOR is a binary serialization of RDF based on the Concise Binary Object Representation (CBOR) as defined by RFC 8949. Like CBOR the serialization is optimized for small code size and fairly small message size. The serialization is suitable for systems and devices that are possibly constrained in terms of network or computation.

RDF/CBOR can also be used to make RDF content-addressable - an identifier for a group of statements can be deterministically derived from the statements themselves.

Installation

pip install rdflib-rdfcbor

Usage

from rdflib import Graph
import rdflib_rdfcbor

# Create a Graph and load some triples from a Turtle file
g1 = Graph()
g1.parse("./test/example.ttl")

# Serialize to RDF/CBOR
cbor_bytes = g1.serialize(format="rdf-cbor", encoding="ascii")

print("RDF/CBOR bytes (in hex): ", cbor_bytes.hex())

# Parse triples from encoded bytes
g2 = Graph()
g2.parse(data=cbor_bytes, format="rdf-cbor")

Content-addressable RDF

RDF/CBOR allows content-addressed RDF by defining a grouping of statements as well as a canonical serialization (see Content-addressable Molecule in the RDF/CBOR specification). This allows distributed systems to compute the same identifier for the same RDF satements independently.

RDF can be content-addressed by either creating a content-addressable graph from scratch or by content-addressing a graph that is not content-addressed.

From scratch

from rdflib import RDF, Literal
from rdflib.namespace import FOAF
import rdflib_rdfcbor.content_addressable

m = RDFCBORContentAddressableMolecule()

m.add(RDF.type, FOAF.Person)
m.add(FOAF.nick, Literal("pukkamustard"))

print(m.base_subject())
# urn:blake2b:QG75V2OGPFMURZTI5U6DJIWGP6JISNGVAYL5C2KDHULVBZXXZUUQ

Content-addressing an existing graph

import rdflib_rdfcbor.content_addressable

for m in rdflib_rdfcbor.content_addressable.of_graph(graph):
    g = Graph()
    for t in m.triples():
        g.add(t)
    print(g.serialize())

This works by first splitting the graph into fragment-molecules and then content-addressing them sequentially. As references between fragments are replaced with the new computed identifiers this only works if there are no cycles between fragment-molecules.

Publishing to PyPi

pip install build twine

# Build the package
python -m build

# Upload using twine
twine upload dist/*

Acknowledgements

This software was initially developed as part of the SNSF-Ambizione funded research project "Computing the Social. Psychographics and Social Physics in the Digital Age".

License

AGPL-3.0-or-later

Release files for rdflib-rdfcbor 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rdflib-rdfcbor 0.1.0
File Size Uploaded
rdflib_rdfcbor-0.1.0.tar.gz 31.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rdflib-rdfcbor 0.1.0
File Interpreter ABI Platform
rdflib_rdfcbor-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 66.1 kB

Release files / rdflib_rdfcbor-0.1.0.tar.gz

Download URL rdflib_rdfcbor-0.1.0.tar.gz
Size 31.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e58b6661517cf6113f5875fcd38cdef598a9b296f9e402a24bf9f58f5a44cea5
BLAKE2b-256 checksum
How to use checksums
4ec2abc889b9621d5b3c0cd7cec12d6087711df8e25cdb43f1aec33ac05eb2ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.7

Release files / rdflib_rdfcbor-0.1.0-py3-none-any.whl

Download URL rdflib_rdfcbor-0.1.0-py3-none-any.whl
Size 34.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85747194c018987a105552fd78dedbbae56a2eea6284a2bbe2d15ca54e3988a3
BLAKE2b-256 checksum
How to use checksums
33ab17d0acba0020a53b6bd56cf177d25f4376ab9abd70f9be61e3a45fdf2295
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page