Skip to content

Instantly share code, notes, and snippets.

@lawlesst
Forked from tatiana/rdfalchemy_test_dbpedia2.py
Created November 16, 2012 12:19
Show Gist options
  • Save lawlesst/4086861 to your computer and use it in GitHub Desktop.
Save lawlesst/4086861 to your computer and use it in GitHub Desktop.
Trying to replace SPARQL query using RDFAlchemy's rdfSubject
"""
See RDFAlchemy-dev list discussion:
https://groups.google.com/forum/?hl=en&fromgroups=#!topic/rdfalchemy-dev/D1AyXQwmOuw
"""
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