RDF Model#

Oxigraph provides python classes to represents basic RDF concepts:

IRIs#

class pyoxigraph.NamedNode(value)#

An RDF node identified by an IRI.

Parameters:

value (str) – the IRI as a string.

Raises:

ValueError – if the IRI is not valid according to RFC 3987.

The str() function provides a serialization compatible with NTriples, Turtle, and SPARQL:

>>> str(NamedNode('http://example.com'))
'<http://example.com>'
value#
Returns:

the named node IRI.

Return type:

str

>>> NamedNode("http://example.com").value
'http://example.com'

Blank Nodes#

class pyoxigraph.BlankNode(value=None)#

An RDF blank node.

Parameters:

value (str or None, optional) – the blank node ID (if not present, a random blank node ID is automatically generated).

Raises:

ValueError – if the blank node ID is invalid according to NTriples, Turtle, and SPARQL grammars.

The str() function provides a serialization compatible with NTriples, Turtle, and SPARQL:

>>> str(BlankNode('ex'))
'_:ex'
value#
Returns:

the blank node ID.

Return type:

str

>>> BlankNode("ex").value
'ex'

Literals#

class pyoxigraph.Literal(value, *, datatype=None, language=None)#

An RDF literal.

Parameters:
Raises:

ValueError – if the language tag is not valid according to RFC 5646 (BCP 47).

The str() function provides a serialization compatible with NTriples, Turtle, and SPARQL:

>>> str(Literal('example'))
'"example"'
>>> str(Literal('example', language='en'))
'"example"@en'
>>> str(Literal('11', datatype=NamedNode('http://www.w3.org/2001/XMLSchema#integer')))
'"11"^^<http://www.w3.org/2001/XMLSchema#integer>'
datatype#
Returns:

the literal datatype IRI.

Return type:

NamedNode

>>> Literal('11', datatype=NamedNode('http://www.w3.org/2001/XMLSchema#integer')).datatype
<NamedNode value=http://www.w3.org/2001/XMLSchema#integer>
>>> Literal('example').datatype
<NamedNode value=http://www.w3.org/2001/XMLSchema#string>
>>> Literal('example', language='en').datatype
<NamedNode value=http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>
language#
Returns:

the literal language tag.

Return type:

str or None

>>> Literal('example', language='en').language
'en'
>>> Literal('example').language
value#
Returns:

the literal value or lexical form.

Return type:

str

>>> Literal("example").value
'example'

Triples#

class pyoxigraph.Triple(subject, predicate, object)#

An RDF triple.

Parameters:

The str() function provides a serialization compatible with NTriples, Turtle, and SPARQL:

>>> str(Triple(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1')))
'<http://example.com> <http://example.com/p> "1"'

A triple could also be easily destructed into its components:

>>> (s, p, o) = Triple(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'))
object#
Returns:

the triple object.

Return type:

NamedNode or BlankNode or Literal or Triple

>>> Triple(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1')).object
<Literal value=1 datatype=<NamedNode value=http://www.w3.org/2001/XMLSchema#string>>
predicate#
Returns:

the triple predicate.

Return type:

NamedNode

>>> Triple(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1')).predicate
<NamedNode value=http://example.com/p>
subject#
Returns:

the triple subject.

Return type:

NamedNode or BlankNode or Triple

>>> Triple(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1')).subject
<NamedNode value=http://example.com>

Quads (triples in a RDF dataset)#

class pyoxigraph.Quad(subject, predicate, object, graph_name=None)#

An RDF triple. in a RDF dataset.

Parameters:

The str() function provides a serialization compatible with NTriples, Turtle, and SPARQL:

>>> str(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g')))
'<http://example.com> <http://example.com/p> "1" <http://example.com/g>'
>>> str(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), DefaultGraph()))
'<http://example.com> <http://example.com/p> "1"'

A quad could also be easily destructed into its components:

>>> (s, p, o, g) = Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g'))
graph_name#
Returns:

the quad graph name.

Return type:

NamedNode or BlankNode or DefaultGraph

>>> Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g')).graph_name
<NamedNode value=http://example.com/g>
object#
Returns:

the quad object.

Return type:

NamedNode or BlankNode or Literal or Triple

>>> Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g')).object
<Literal value=1 datatype=<NamedNode value=http://www.w3.org/2001/XMLSchema#string>>
predicate#
Returns:

the quad predicate.

Return type:

NamedNode

>>> Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g')).predicate
<NamedNode value=http://example.com/p>
subject#
Returns:

the quad subject.

Return type:

NamedNode or BlankNode or Triple

>>> Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g')).subject
<NamedNode value=http://example.com>
triple#
Returns:

the quad underlying triple.

Return type:

Triple

>>> Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g')).triple
<Triple subject=<NamedNode value=http://example.com> predicate=<NamedNode value=http://example.com/p> object=<Literal value=1 datatype=<NamedNode value=http://www.w3.org/2001/XMLSchema#string>>>
class pyoxigraph.DefaultGraph#

The RDF default graph name.

value#
Returns:

the empty string.

Return type:

str