The Describer class

class ferenda.Describer(graph=None, about=None, base=None)[source]

Extends the utility class rdflib.extras.describer.Describer so that it reads values and refences as well as write them.

Parameters:
  • graph (Graph) – The graph to read from and write to
  • about (string or Identifier) – the current subject to use
  • base (string) – Base URI for any relative URIs used with about(), rel() or rev(),
getvalues(p)[source]

Get a list (possibly empty) of all literal values for the given property and the current subject. Values will be converted to plain literals, i.e. not rdflib.term.Literal objects.

Parameters:p (rdflib.term.URIRef) – The property of the sought literal.
Returns:a list of matching literals
Return type:list of strings (or other appropriate python type if the literal has a datatype)
getrels(p)[source]

Get a list (possibly empty) of all URIs for the given property and the current subject. Values will be converted to strings, i.e. not rdflib.term.URIRef objects.

Parameters:p (rdflib.term.URIRef) – The property of the sought URI.
Returns:The matching URIs
Return type:list of strings
getrdftype()[source]

Get the rdf:type of the current subject.

Returns:The URI of the current subjects’s rdf:type.
Return type:string
getvalue(p)[source]

Get a single literal value for the given property and the current subject. If the graph contains zero or more than one such literal, a KeyError will be raised.

Note

If this is all you use Describer for, you might want to use rdflib.graph.Graph.value() instead – the main advantage that this method has is that it converts the return value to a plain python object instead of a rdflib.term.Literal object.

Parameters:p (rdflib.term.URIRef) – The property of the sought literal.
Returns:The sought literal
Return type:string (or other appropriate python type if the literal has a datatype)
getrel(p)[source]

Get a single URI for the given property and the current subject. If the graph contains zero or more than one such URI, a KeyError will be raised.

Parameters:p (rdflib.term.URIRef) – The property of the sought literal.
Returns:The sought URI
Return type:string
about(subject, **kws)[source]

Sets the current subject. Will convert the given object into an URIRef if it’s not an Identifier.

Usage:

>>> d = Describer()
>>> d._current() 
rdflib.term.BNode(...)
>>> d.about("http://example.org/")
>>> d._current()
rdflib.term.URIRef('http://example.org/')
rdftype(t)[source]

Shorthand for setting rdf:type of the current subject.

Usage:

>>> from rdflib import URIRef
>>> from rdflib.namespace import RDF, RDFS
>>> d = Describer(about="http://example.org/")
>>> d.rdftype(RDFS.Resource)
>>> (URIRef('http://example.org/'),
...     RDF.type, RDFS.Resource) in d.graph
True
rel(p, o=None, **kws)[source]

Set an object for the given property. Will convert the given object into an URIRef if it’s not an Identifier. If none is given, a new BNode is used.

Returns a context manager for use in a with block, within which the given object is used as current subject.

Usage:

>>> from rdflib import URIRef
>>> from rdflib.namespace import RDF, RDFS
>>> d = Describer(about="/", base="http://example.org/")
>>> _ctxt = d.rel(RDFS.seeAlso, "/about")
>>> d.graph.value(URIRef('http://example.org/'), RDFS.seeAlso)
rdflib.term.URIRef('http://example.org/about')

>>> with d.rel(RDFS.seeAlso, "/more"):
...     d.value(RDFS.label, "More")
>>> (URIRef('http://example.org/'), RDFS.seeAlso,
...         URIRef('http://example.org/more')) in d.graph
True
>>> d.graph.value(URIRef('http://example.org/more'), RDFS.label)
rdflib.term.Literal('More')
rev(p, s=None, **kws)[source]

Same as rel, but uses current subject as object of the relation. The given resource is still used as subject in the returned context manager.

Usage:

>>> from rdflib import URIRef
>>> from rdflib.namespace import RDF, RDFS
>>> d = Describer(about="http://example.org/")
>>> with d.rev(RDFS.seeAlso, "http://example.net/"):
...     d.value(RDFS.label, "Net")
>>> (URIRef('http://example.net/'), RDFS.seeAlso,
...         URIRef('http://example.org/')) in d.graph
True
>>> d.graph.value(URIRef('http://example.net/'), RDFS.label)
rdflib.term.Literal('Net')
value(p, v, **kws)[source]

Set a literal value for the given property. Will cast the value to an Literal if a plain literal is given.

Usage:

>>> from rdflib import URIRef
>>> from rdflib.namespace import RDF, RDFS
>>> d = Describer(about="http://example.org/")
>>> d.value(RDFS.label, "Example")
>>> d.graph.value(URIRef('http://example.org/'), RDFS.label)
rdflib.term.Literal('Example')