2

I would like to retrieve property/object pairs of a given subject, but only for these properties matching a specific namespace (let us say rdflib.RDFS.)

Something like g.predicate_objects(s, prop_prefix=RDFS).

Is there a built-in way to achieve that, or should I rather retrieve all pairs and filter myself on the property IRI?

Florent Georges
  • 2,190
  • 1
  • 15
  • 24
  • Use SPARQL (perhaps you need a subquery in order to improve performance): `FILTER strstarts(str(?p), str(rdfs:))` or just hardcode all predicates from, say, RDFS in `VALUES`. – Stanislav Kralin Sep 26 '20 at 17:30
  • Thank you. But unless there was a builtin function to do just that, the easiest way (at least in my case) was simply to use `for p, o in g.predicate_objects(s):` and filter at the top of the loop. – Florent Georges Sep 29 '20 at 15:44

2 Answers2

1

With rdflib.Namespace extenting str, this works:

for subject, predicate, object in graph.triples((None, None, None)):
    assert isinstance(predicate, rdflib.URIRef)
    if predicate.startswith(rdflib.RDFS):
        ...  # predicate is from RDFS namespace
K3---rnc
  • 6,717
  • 3
  • 31
  • 46
0

To "filter at the top of the loop" is the only way to go given there is no RDFlib method for this.

(I'm just replying here echoing the comments so this question can be answered and thus closed)

Nicholas Car
  • 1,164
  • 4
  • 7