Using RDF with Python
Nathan R. Yergler
Creative Commons
What This Talk Is
- Brief overview of RDF
- Very brief talk about why RDF matters
- Overview of your options as Python developers
What Is RDF?
- The short answer:
An model for describing relationships and information, often encoded in XML.
- The long answer:
RDF has an abstract syntax that reflects a simple graph-based data model, and formal semantics with a rigorously defined notion of entailment providing a basis for well founded deductions in RDF data.
RDF Statements
- Statements consisting of "triples"
- Subject, Predicate, Object
- Statements can be simple declarations:
Nathan owns a dog.
- ...or containers, sequences, etc:
The names of Nathan's dogs are Bosco and Madeline
Sample RDF Block
<rdf:RDF xmlns="http://web.resource.org/cc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Work rdf:about="">
<license rdf:resource="http://creativecommons.org/licenses/by/2.0/" />
</Work>
<License rdf:about="http://creativecommons.org/licenses/by/2.0/">
<permits rdf:resource="http://web.resource.org/cc/Reproduction" />
<permits rdf:resource="http://web.resource.org/cc/Distribution" />
<requires rdf:resource="http://web.resource.org/cc/Notice" />
<requires rdf:resource="http://web.resource.org/cc/Attribution" />
<permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
</License>
</rdf:RDF>
Why is RDF Important?
- Objects get a fully qualified URI (http://web.resource.org/cc/License)
- Allows you to relate physically (and logically) disparate islands of information
- Allows the modeling of complex relationships
- Standardized model, so we can programmatically explore relationships
RDF In Use Today
- Creative Commons license declarations
- Edd Dumbhill's DOAP
- FOAF
rdflib
- Largest, most diverse RDF handling library
- Create RDF by making assertions
>>> store = TripleStore()
>>> store.parse ( inputsource )
>>> rdf_subjects = store.subjects()
>>> store.predicates(subject="http://web.resource.org/cc/License")
pyrple
- Lightweight, "experimental" API
- In-memory triple store
- Very slick graph rule testing
>>> from pyrple import Triple
>>> from pyrple.namespaces import FOAF, VAR
>>> Triple(VAR.someone, FOAF.knows, VAR.person)
(?someone, , ?person)
pyrple
- Graph class represents a collection of triples
query method returns list of triples which match the query parameter
sparta
- Wrapper around rdflib's TripleStore
- Maps RDF types to Python types
- Needs to bootstrap the TripleStore before using sparta methods
from rdflib.TripleStore import TripleStore
store = TripleStore()
store.parse([URI])
store.prefix_mapping([prefix], [URI])
Thing = ThingFactory(store)
Real example... ccRdf
- Used for CC Applications that need to manipulate/read license data.
- Wraps rdflib with a dict-like interface
>>> rdf_string = """... rdf goes here ..."""
>>> ccr = ccRdf()
>>> ccr.parse(rdf_string)
>>> license = ccr.licenses()[0]
>>> licenses['requires']
['Attribution']
Conclusions
- RDF is a way of thinking...
- Not all problems have been solved yet
- See the wiki for more information