Skip to content

Instantly share code, notes, and snippets.

@nutjob4life
Created November 6, 2013 21:34
Show Gist options
  • Select an option

  • Save nutjob4life/7344503 to your computer and use it in GitHub Desktop.

Select an option

Save nutjob4life/7344503 to your computer and use it in GitHub Desktop.
Example of making an RDF "Bag" with rdflib in Python
# encoding: utf-8
import rdflib
book = rdflib.URIRef(u'urn:isbn:978-3-16-148410-0')
bookType = rdflib.URIRef(u'urn:publishing:schema:book')
authorPredicate = rdflib.URIRef(u'http://purl.org/dc/terms/creator')
joyce = rdflib.URIRef(u'urn:foaf:registered-people:joyce')
kelly = rdflib.URIRef(u'http://twitter.org/nutjob4life')
mattmann = rdflib.URIRef(u'http://people.apache.org/mattmann')
# We have a book
g = rdflib.Graph()
g.add((book, rdflib.RDF.type, bookType))
# It has a collection of authors
bag = rdflib.BNode()
g.add((book, authorPredicate, bag))
# The collection is a "Bag"
g.add((bag, rdflib.RDF.type, rdflib.RDF.Bag))
# The bag has 3 authors
g.add((bag, rdflib.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#_1'), joyce))
g.add((bag, rdflib.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#_2'), kelly))
g.add((bag, rdflib.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#_3'), mattmann))
# That's it.
print g.serialize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment