Pierre-Antoine Champin, research professor in University of Lyon 1, recently released a little code that do great things. RDF PyPath is a Python code that allows to use RDF resources directly in your code, by playing with the / operator.
Let’s have a look at the “official” example. To load a RDF resource, begin with:
me = Root(URIRef("http://champin.net/foaf.rdf#pa"), a_graph)
This creates a Root object containing the data of your RDF file. You can then manipulate the data easily, with a XPath-like syntax. For example:
names = me/FOAF.knows/FOAF.name
This gets the names of all the people Pierre-Antoine knows. You can also go further and use conditions in the same way as XPath. To get all Pierre-Antoine’s friends that are named John, you can do the following:
johns = me/foaf:knows[X/foaf:name == "John"]
Isn’t it amazing? And I guess this can be very useful when using linked data in Python.