Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (99 sloc) 4.798 kb
f0775ae @mnot tweak readme
authored
1 ======
38361c6 @mnot first commit
authored
2 Sparta
3 ======
4
5 A Simple API for RDF by Mark Nottingham, <mnot@pobox.com>
6
f6250f0 @mnot Update for rdflib 2.4; fix _id typo
authored
7 Sparts is a simple, resource-centric API for RDF graphs, built on top of
526bb99 @mnot readme typos, formatting
authored
8 rdflib_.
f6250f0 @mnot Update for rdflib 2.4; fix _id typo
authored
9
38361c6 @mnot first commit
authored
10 Installation
11 ------------
12
f6250f0 @mnot Update for rdflib 2.4; fix _id typo
authored
13 Sparta requires rdflib_ 2.4+.
14
38361c6 @mnot first commit
authored
15 To install::
f0775ae @mnot tweak readme
authored
16
38361c6 @mnot first commit
authored
17 python setup.py install
18
19 For installation help::
f0775ae @mnot tweak readme
authored
20
38361c6 @mnot first commit
authored
21 python setup.py install --help
22
23 In examples/::
f0775ae @mnot tweak readme
authored
24
38361c6 @mnot first commit
authored
25 example.py: Example of use
26 example-out.txt: ouput of example script
27 rss.py: example RSS 1.0 feed parser
28 rss_schema.xml: partial RDF Schema/OWL for RSS 1.0 example
29
d85a4f4 @mnot update sparta URL
authored
30 For more information, see sparta.py and <http://github.com/mnot/sparta/>.
38361c6 @mnot first commit
authored
31
32 Getting Started
33 ---------------
34
35 The easiest way to get started is to play with it; take a look at the example
36 files above. You can also take a look through the preliminary documentation below.
37
f6250f0 @mnot Update for rdflib 2.4; fix _id typo
authored
38 Sparta is a wrapper around an rdflib_ Graph. To use it,
38361c6 @mnot first commit
authored
39 you must first instantiate one or more Graphs, make any necessary prefix mappings,
40 and then instantiate a ThingFactory.
41
42 Prefix bindings allow URIs to be referred to with a short name.
43 For example, if "http://www.example.com/foo#" is mapped to the prefix "foo",
44 then the URI "http://www.example.com/foo#bar" can be referred to in Sparta
45 with the name "foo_bar".
46
47 Prefix bindings are made by calling the normal bind(prefix,
48 URI) method on the rdflib graph. You can also bind a complete URI to a
49 string with the addAlias(alias, URI) method on the ThingFactory
50 (this accommodates URIs that are awkward or impossible to map using
51 prefixes).
52
53 To be instantiated, a ThingFactory requires one argument; the
54 Graph (or store) that is to be used. Optionally, you can also give a
55 schema_store argument, which points to a separate store that contains the
56 schema hints used to help Sparta map RDF into Python datatypes and objects. If
57 this is not specified, the primary store will be used.
58
59 This is a common idiom for setting up Sparta::
60
61 <pre class="example"> from rdflib.Graph import Graph
62 store = Graph()
63 store.parse([URI])
64 store.bind([prefix], [URI])
65 Thing = ThingFactory(store)</pre>
66
67 Working with Nodes
68 ------------------
69
70 Once you've bound any prefixes you need and set up the store,
71 you're ready to work with RDF data.
72
73 An RDF node is represented as a Python object in Sparta, whose properties
74 correspond to RDF arcs. To start working with a node, you must instantiate it
75 with its identity; there are three ways to do this.
76
526bb99 @mnot readme typos, formatting
authored
77 1. Thing("prefix_localname") - Refers to the URI indicated using the
78 prefix mapping, as described above.
79 2. Thing(URIRef('http://www.example.com/foo#bar')) - Refers to the
80 URI specified.
81 3. Thing(None) - creates a bNode_ (blank, or anonymous RDF node).
38361c6 @mnot first commit
authored
82
83 Accessing and Manipulating Data
84 -------------------------------
85
86 A node's properties can be accessed and changed by name,
87 using the prefix mapping as explained above. For example::
88
89 print foo.rdf_type
90
91 will print the 'rdf_type' property of the 'foo' node.
92
93 There are two ways to access a property's values, depending on what Sparta
94 knows about it through the schema store. If it is an
a1f2bce @mnot more readme cleanups
authored
95 owl:FunctionalProperty_, or if the subject is subclassed to restrict that
96 property with either a owl:maxCardinality_ or a owl:cardinality_ of "1", the
97 property can be accessed as a normal, singular value; that is, it can be
98 accessed as explained above, assigned with the '=' operator, deleted with
38361c6 @mnot first commit
authored
99 'del', and so forth.
100
101 Otherwise, the property's value is assumed to have a cardinality greater
a1f2bce @mnot more readme cleanups
authored
102 than one, and implements a subset of the Python set() interface. For
38361c6 @mnot first commit
authored
103 example, you can add to the set with the add method, like this::
104
105 foo.children.add("bob")
106
107 test for membership with the in operator, and so forth. See the PropertySet
108 class for the exact methods implemented.
109
110 Datatyping
111 ----------
112
113 An RDF predicate with one of the following as its
6721f84 @mnot readme: rdfs:range
authored
114 rdfs:range_ (according to the schema store) will be mapped to these
115 Python datatypes:
38361c6 @mnot first commit
authored
116
526bb99 @mnot readme typos, formatting
authored
117 * rdf:List - list
118 * rdf:Seq - list
119 * xs:string, xs:normalizedString, xs:token, xs:language - unicode
120 * xs:boolean - bool
121 * xs:decimal, xs:float, xs:double - float
122 * xs:integer, xs:long, xs:unsignedLong, xs:unsignedInt - long
123 * xs:nonPositiveInteger, xs:nonNegativeInteger, xs:positiveInteger,
124 xs:negativeInteger, xs:int, xs:short, xs:byte, xs:unsignedShort,
125 xs:unsignedByte - int
126 * xs:anyURI - str
127 * xs:base64Binary - (decoded base64)
f6250f0 @mnot Update for rdflib 2.4; fix _id typo
authored
128
129
a1f2bce @mnot more readme cleanups
authored
130 .. _rdflib: http://rdflib.net/
131 .. _bnode: http://www.w3.org/TR/rdf-primer/#structuredproperties
132 .. _cardinality: http://www.w3.org/TR/owl-ref/#cardinality
133 .. _maxCardinality: http://www.w3.org/TR/owl-ref/#maxCardinality-def
134 .. _FunctionalProperty: http://www.w3.org/TR/owl-ref/#FunctionalProperty-def
6721f84 @mnot readme: rdfs:range
authored
135 .. _range: http://www.w3.org/TR/rdf-schema/#ch_range
Something went wrong with that request. Please try again.