rdflib Package¶
rdflib Package¶
A pure Python package providing the core RDF constructs.
The packages is intended to provide the core RDF types and interfaces for working with RDF. The package defines a plugin interface for parsers, stores, and serializers that other packages can use to implement parsers, stores, and serializers that will plug into the rdflib package.
The primary interface rdflib exposes to work with RDF is rdflib.graph.Graph.
A tiny example:
>>> from rdflib import Graph, URIRef, Literal
>>> g = Graph()
>>> result = g.parse("http://www.w3.org/2000/10/swap/test/meet/blue.rdf")
>>> print("graph has %s statements." % len(g))
graph has 4 statements.
>>>
>>> for s, p, o in g:
... if (s, p, o) not in g:
... raise Exception("It better be!")
>>> s = g.serialize(format='nt')
>>>
>>> sorted(g) == [
... (URIRef(u'http://meetings.example.com/cal#m1'),
... URIRef(u'http://www.example.org/meeting_organization#homePage'),
... URIRef(u'http://meetings.example.com/m1/hp')),
... (URIRef(u'http://www.example.org/people#fred'),
... URIRef(u'http://www.example.org/meeting_organization#attending'),
... URIRef(u'http://meetings.example.com/cal#m1')),
... (URIRef(u'http://www.example.org/people#fred'),
... URIRef(u'http://www.example.org/personal_details#GivenName'),
... Literal(u'Fred')),
... (URIRef(u'http://www.example.org/people#fred'),
... URIRef(u'http://www.example.org/personal_details#hasEmail'),
... URIRef(u'mailto:fred@example.com'))
... ]
True
-
rdflib.__init__.NORMALIZE_LITERALS= True¶ If True - Literals lexical forms are normalized when created. I.e. the lexical forms is parsed according to data-type, then the stored lexical form is the re-serialized value that was parsed.
Illegal values for a datatype are simply kept. The normalized keyword for Literal.__new__ can override this.
For example:
>>> from rdflib import Literal,XSD >>> Literal("01", datatype=XSD.int) rdflib.term.Literal(u'1', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#integer'))
This flag may be changed at any time, but will only affect literals created after that time, previously created literals will remain (un)normalized.
-
rdflib.__init__.DAWG_LITERAL_COLLATION= False¶ DAWG_LITERAL_COLLATION determines how literals are ordered or compared to each other.
In SPARQL, applying the >,<,>=,<= operators to literals of incompatible data-types is an error, i.e:
Literal(2)>Literal(‘cake’) is neither true nor false, but an error.
This is a problem in PY3, where lists of Literals of incompatible types can no longer be sorted.
Setting this flag to True gives you strict DAWG/SPARQL compliance, setting it to False will order Literals with incompatible datatypes by datatype URI
In particular, this determines how the rich comparison operators for Literal work, eq, __neq__, __lt__, etc.
collection Module¶
-
class
rdflib.collection.Collection(graph, uri, seq=[])[source]¶ Bases:
objectSee 3.3.5 Emulating container types: http://docs.python.org/ref/sequence-types.html#l2h-232
>>> from rdflib.graph import Graph >>> from pprint import pprint >>> listName = BNode() >>> g = Graph('IOMemory') >>> listItem1 = BNode() >>> listItem2 = BNode() >>> g.add((listName, RDF.first, Literal(1))) >>> g.add((listName, RDF.rest, listItem1)) >>> g.add((listItem1, RDF.first, Literal(2))) >>> g.add((listItem1, RDF.rest, listItem2)) >>> g.add((listItem2, RDF.rest, RDF.nil)) >>> g.add((listItem2, RDF.first, Literal(3))) >>> c = Collection(g,listName) >>> pprint([term.n3() for term in c]) [u'"1"^^<http://www.w3.org/2001/XMLSchema#integer>', u'"2"^^<http://www.w3.org/2001/XMLSchema#integer>', u'"3"^^<http://www.w3.org/2001/XMLSchema#integer>']
>>> Literal(1) in c True >>> len(c) 3 >>> c._get_container(1) == listItem1 True >>> c.index(Literal(2)) == 1 True
-
__delitem__(key)[source]¶ >>> from rdflib.namespace import RDF, RDFS >>> from rdflib import Graph >>> from pprint import pformat >>> g = Graph() >>> a = BNode('foo') >>> b = BNode('aoo') >>> b = BNode(self): if self
Literal("01"rdflibss="s2">"01"minCardinality("graph has %s statements." +9lass="p">)) == 1RDF, RDFS from rdflib.namespace , RDF.... BNode('foo')2'foo')2'foospan class= lexical formclass="o">.bind(prefixNext topic 'aoo')add((listItem2, RDF.
-