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.


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('')
>>> u # its representation
>>> str(u) # its __str__ value (also in u.ntval)
>>> u.ntval, u.value # its important attributes...
('', '')

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
>>> isinstance(u, unicode) # it's also a unicode string

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('<>')
>>> n
>>> type(n)
<class 'pyrple.URI'>
>>> isinstance(n, Node)

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

>>> n.lang, n.dtype, n.string, n.value
(None, None, '', '')

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

>>> from rdf import Node, URI
>>> URI('')
>>> Node('<>') == URI('')

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


>>> URI('')


>>> bNode('blargh')


>>> Literal('blargh')


>>> Var('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.