rdf in python

For Johan,

Was looking through the citeproc-py code. Great to see the RDF driver.
However, you might want to look at this example for a perhaps easier
and more flexible approach (because completely decoupled from
serialization format):


So here he’s using RDFLib to do the RDF parsing, and then mapping the
triples to an object.

Another (more generic) alternative, is this:


Example code I was playing with:

#!/usr/bin/env python

encoding: utf-8

from config.namespaces import BIBO, DC
from rdfalchemy import rdfSubject, rdfSingle, rdfMultiple

class Document(rdfSubject):
rdf_type = BIBO.Document
title = rdfSingle(DC.title)
date = rdfSingle(DC.date)
issued = rdfSingle(DC.issued)
peer_reviewed = rdfSingle(BIBO.peerReviewed)
# for convenience, we leave room for a more denormalized representation
creators = rdfMultiple(DC.creator)
contributions = rdfMultiple(BIBO.contribution)
subjects = rdfMultiple(DC.subject)

def concept_sort(a,b):
    return cmp(a.pref_label, b.pref_label)

# self.subjects.sort(concept_sort)

class Article(Document):
volume = rdfSingle(BIBO.volume)
issue = rdfSingle(BIBO.issue)
pages = rdfSingle(BIBO.pages)
page_start = rdfSingle(BIBO.pageStart)
page_end = rdfSingle(BIBO.pageEnd)
periodical = rdfSingle(DC.isPartOf)

class AcademicArticle(Article):

class Chapter(Document):
# reference to a Book
published_in = rdfSingle(DC.isPartOf)

class Review(Article):
# reviewed resource
review_of = rdfSingle(BIBO.reviewOf)

class PublishedDocument(Document):
publisher = rdfSingle(DC.publisher)

class Book(PublishedDocument):


Hi Bruce,

Thanks, that does seem quite interesting. I do not quite grasp what is
going on in the code from the first link. However, RDFAlchemy seems an
interesting module indeed. I haven’t looked at the RDF driver in a
while. I first want to finish the BibTex driver. I’ll take a look at
implementing the RDF driver after that.


I take it you mean code like this?

    for p, o in graph.predicate_objects(subject=uri):
        if p == SKOS.prefLabel:
            self.pref_label = o

The predicte_objects method is just finding all triple statements
related to each RDF subject (identifier with a URI).

So if you have triple statements of the form:


The above could be rewritten as:

    for predicate, object in graph.predicate_objects(subject=uri):
        if predicate == SKOS.prefLabel:
            self.pref_label = object

If you then have a python object “x”, you access the content of the
pref_lable triple object like:


That make sense?


simon & bruce–

i was checking complaints in this forum:

and it turns out that the existing APA style has quite a few bugs. i’d
like to fix it (that would require rewriting several macros)–do you
have any objections?


Not at all!