sparta

a Simple API for RDF

Sparta is an Python API for RDF that is designed to help easily learn and navigate the Semantic Web programmatically. Unlike other RDF interfaces, which are generally triple-based, Sparta binds RDF nodes to Python objects and RDF arcs to attributes of those Python objects.

This makes using RDF very natural for people who understand (and sometimes think in terms of) objects. One way to think of it is as a “data binding” from RDF to Python objects.

Sparta requires rdflib version 2.3.1 or greater, and is released under an MIT license.

Obtaining Sparta

The latest version can be downloaded as a plain file, or as a full Python installer, from the dist directory below.

Icon  Name                                     Size  Description
[DIR] dist/ - Releases [TXT] example-out.txt 4.1K Example output [   ] example.py 3.4K Informal test / demo [   ] rss.py 839 RSS 1.0 parser [   ] rss_schema.xml 691 RSS 1.0 RDF Schema (partial; for rss_test) [   ] sparta.py 20K Latest source, for browsing

Getting Started

The easiest way to get started is to play with it; take a look at the example files above. You can also take a look through the preliminary documentation below.

Sparta is a wrapper around an rdflib Graph. To use it, you must first instantiate one or more Graphs, make any necessary prefix mappings, and then instantiate a ThingFactory.

Prefix bindings allow URIs to be referred to with a short name. For example, if "http://www.example.com/foo#" is mapped to the prefix "foo", then the URI "http://www.example.com/foo#bar" can be referred to in Sparta with the name "foo_bar".

Prefix bindings are made by calling the normal bind(prefix, URI) method on the rdflib graph. You can also bind a complete URI to a string with the addAlias(alias, URI) method on the ThingFactory (this accommodates URIs that are awkward or impossible to map using prefixes).

To be instantiated, a ThingFactory requires one argument; the Graph (or store) that is to be used. Optionally, you can also give a schema_store argument, which points to a separate store that contains the schema hints used to help Sparta map RDF into Python datatypes and objects. If this is not specified, the primary store will be used.

This is a common idiom for setting up Sparta;

    from rdflib.Graph import Graph
    store = Graph()
    store.parse([URI])
    store.bind([prefix], [URI])
    Thing = ThingFactory(store)

Working with Nodes

Once you’ve bound any prefixes you need and set up the store, you’re ready to work with RDF data.

An RDF node is represented as a Python object in Sparta, whose properties correspond to RDF arcs. To start working with a node, you must instantiate it with its identity; there are three ways to do this.

Thing("prefix_localname")
Refers to the URI indicated using the prefix mapping, as described above.
Thing(URIRef('http://www.example.com/foo#bar'))
Refers to the URI specified.
Thing(None)
This creates a bNode (blank, or anonymous RDF node).

Accessing and Manipulating Data

A node’s properties can be accessed and changed by name, using the prefix mapping as explained above. For example,

print foo.rdf_type

will print the 'rdf_type' property of the 'foo' node.

There are two ways to access a property's values, depending on what Sparta knows about it through the schema store. If it is an owl: FunctionalProperty, or if the subject is subclassed to restrict that property with either a owl:maxCardinality or a owl:cardinality of "1", the property can be accessed as a normal, singular value; that is, it can be accessed as explained above, assigned with the '=' operator, deleted with 'del', and so forth.

Otherwise, the property's value is assumed to have a cardinality greater than one, and implements a subset of the sets.Set interface. For example, you can add to the set with the add method, like this;

foo.children.add("bob")

test for membership with the in operator, and so forth. See the PropertySet class for the exact methods implemented.

Datatyping

An RDF predicate with one of the following as its rdfs:range (according to the schema store) will be mapped to these Python datatypes;

rdf:List
list
rdf:Seq
list
xs:string, xs:normalizedString, xs:token, xs:language
unicode
xs:boolean
bool
xs:decimal, xs:float, xs:double
float
xs:integer, xs:long, xs:unsignedLong, xs:unsignedInt
long
xs:nonPositiveInteger, xs:nonNegativeInteger, xs:positiveInteger, xs:negativeInteger, xs:int, xs:short, xs:byte, xs:unsignedShort, xs:unsignedByte
int
xs:anyURI
str
xs:base64Binary
(decoded base64)