Skip to content

Instantly share code, notes, and snippets.

@tatiana
Created June 22, 2012 19:14
Show Gist options
  • Save tatiana/2974582 to your computer and use it in GitHub Desktop.
Save tatiana/2974582 to your computer and use it in GitHub Desktop.
Trying to replace SPARQL query using RDFAlchemy's rdfSubject
from rdfalchemy import rdfSingle, rdfSubject
from rdfalchemy.sparql import SPARQLGraph
from rdflib import Namespace
query = """
PREFIX db: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX dbonto: <http://dbpedia.org/ontology/>
SELECT distinct ?name ?label {
?artist
a dbonto:MusicalArtist;
dbprop:birthName ?name.
{
?artist dbonto:instrument db:Guitar .
db:Guitar rdfs:label ?label.
}
UNION
{
?artist dbonto:instrument db:Piano .
db:Piano rdfs:label ?label .
}
FILTER ( lang(?name) = "en" )
}
ORDER BY ?name
LIMIT 10
"""
DB = Namespace('http://dbpedia.org/resource/')
DBPROP = Namespace('http://dbpedia.org/property/')
DBONTO = Namespace('http://dbpedia.org/ontology/')
RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')
endpoint = "http://live.dbpedia.org/sparql"
graph = SPARQLGraph(endpoint)
rdfSubject.db = graph
class Instrument(rdfSubject):
rdfs_label = rdfSingle(RDFS.label, 'label')
class MusicalArtist(rdfSubject):
rdf_type = DBONTO.MusicalArtist
dbprop_birthname = DBPROP.birthName
dbonto_instrument = Instrument # ?
guitar_players = MusicalArtist.get_by(dbonto_instrument=DB.Guitar)
# Line above throws "AttributeError: type object 'rdfSubject' has no attribute 'pred'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment