CFGraph

raw JSON →
0.2.1 verified Sat May 09 auth: no python

CFGraph is a Python library that provides a flattening graph for RDFlib collections, converting RDF collection structures into a simplified graph representation. Version 0.2.1 is the latest release, with an irregular release cadence.

pip install cfgraph
error ImportError: cannot import name 'Flattener' from 'cfgraph'
cause Incorrect import path; Flattener is at cfgraph.Flattener, not cfgraph.flattener.
fix
Use from cfgraph import Flattener.
error AttributeError: 'Graph' object has no attribute 'first'
cause CFGraph expects standard RDF collection patterns; if your graph does not use rdf:first/rdf:rest, the Flattener may raise this error.
fix
Preprocess data to standard RDF collections, or check if your data uses a different collection pattern.
gotcha The Flattener modifies the graph in-place. Make a copy if you need to preserve the original graph.
fix Use `g2 = rdflib.Graph()` and parse again, or use `g2 = g + g` (but careful with blank nodes).
gotcha CFGraph only works with RDF collections that follow the standard rdf:first/rdf:rest pattern. Other collection representations are not supported.
fix Ensure your data uses the standard RDF collection pattern.
deprecated The library has not been updated in several years and may have compatibility issues with newer versions of rdflib (e.g., rdflib >= 6.0).
fix Use with rdflib < 6.0 or test compatibility.

Basic usage: parse an RDF graph and flatten all collections using the Flattener class.

import rdflib
from cfgraph import Flattener

# Load an RDF graph
g = rdflib.Graph()
g.parse('example.ttl', format='turtle')

# Flatten collections
flattener = Flattener(g)
flattener.flatten()

# The graph now has flattened collections
print(f"Number of triples after flattening: {len(g)}")