pyrple.node

Node()

Nodes are the smallest atomic parts of an RDF system. They come in four different flavours: URI, bNode, Literal, and Var. Each of these are implemented as sub classes of Node.

Generally, it is best, though not harmful, to use the sub classes of Node when using pyrple. Indeed, the Node class constructor will always return one of its four sub classes upon instantiation, and never a plain node.

Usage

The sub classes of Node()--i.e. URI, bNode, Literal, and Var--must be passed a plain value. It is recommended to use them instead of Node when possible. Node() itself is handy for when parsing N-Triples files: the Node() constructor must be passed a valid N-Triples term. It also takes universally quantified variable syntax handy for queries and borrowed from Notation3.

For example:

>>> from pyrple import URI
>>> u = URI('http://www.w3.org/')
>>> u # its representation
<http://www.w3.org/>
>>> str(u) # its __str__ value (also in u.ntval)
'http://www.w3.org/'
>>> u.ntval, u.value # its important attributes...
('http://www.w3.org/', 'http://www.w3.org/')
>>> 

Therefore, to get the N-Triples representation of Node or any subClass thereof, use its __repr__ method (or repr(), or '%r' % node). To get its value, use str(node) or node.value.

Note that it's a URI, a Node, and a unicode string:

>>> type(u)
<class 'pyrple.node.URI'>
>>> isinstance(u, Node) # it's a URI and a Node
True
>>> isinstance(u, unicode) # it's also a unicode string
True

For the URI example, u.ntval and u.value are equivalent. The reason why both are required becomes apparent when using the Literal class:

>>> from pyrple import Literal
>>> lit = Literal('first line\nsecond line')
>>> str(lit)
'first line\\nsecond line'
>>> lit.ntval, lit.value
('first line\\nsecond line', 'first line\nsecond line')
>>> 

lit.ntval is the N-Triples escaped version of the Literal's content, and lit.value is the raw value of its content. Unless you're printing to N-Triples, you'll probably want to access its raw value.

>>> from pyrple import Node
>>> n = Node('<http://www.w3.org/>')
>>> n
<http://www.w3.org/>
>>> type(n)
<class 'pyrple.URI'>
>>> isinstance(n, Node)
True
>>> 

A Node will always have lang, dtype, string, and value properties.

>>> n.lang, n.dtype, n.string, n.value
(None, None, 'http://www.w3.org/', 'http://www.w3.org/')
>>> 

Node must be passed a single N-Triples encoded term; its sub classes take unencoded terms.

>>> from rdf import Node, URI
>>> URI('http://www.w3.org/')
<http://www.w3.org/>
>>> Node('<http://www.w3.org/>') == URI('http://www.w3.org/')
True
>>> 

Here's an example of what each of the sub classes of Node produce:-

URI()

>>> URI('http://www.w3.org/')
<http://www.w3.org/>

bNode()

>>> bNode('blargh')
_:blargh

Literal

>>> Literal('blargh')
"blargh"
>>> 

Var()

>>> Var('blargh')
?blargh

Note again that they produce literals which are valid N-Triples terms, except for Var, which is an extension borrowed from N3 and RDQL for the purposes of querying.