I'm in danger of underusing this blog - I wrote the stuff for this post a while back, but didn't get round to posting it:

Have put together a package of wrapper code I've been developing as I write python RDF applications. It wraps the rdflib and sparta behaviour to present a nice (readable) api. well niceish.


s = Store() s.set_prefix_mapping("ex","http://example.com/") phil = s["ex:Phil"] phil.foaf_name = "Phil Dawes" print s.serialize() <?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:ex="http://example.com/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" > <rdf:Description rdf:about="http://example.com/Phil"> <foaf:name>Phil Dawes</foaf:name> </rdf:Description> </rdf:RDF>

Note that assigning to a property overwrites any existing property statements against the subject. If you don't want to overwrite, you can use the add() method.


phil.add("foaf:nick","Phil") phil.add("foaf:nick","George Dawes - the man with the scores")

gives phil 2 foaf nicknames

You can get an iterator to all the values of foaf:nick by using the get() method:

for nickname in phil.get("foaf:nick"): . print nickname

Resource objects work interchangably with the rdflib search functions

for person in s.subjects(TYPE,s['foaf:Person']): . print person.foaf_name

As an added bonus, I've also added basic n3 parsing support by bolting Sean Palmer's yapps n3 parser to rdflib.

s.load("http://myrdf.com/foo.n3","n3") or s.parse(myN3,"n3")

Hope this helps someone!