Python "wrapper" for Piglet
We are building a "wrapper" for the Piglet library (libpiglet), allowing the creation and use of Piglet RDF triplestores from Python programs. To make use of it, you have to import the module with
import piglet
In Piglet, nodes - resources (including bnodes) and literals - are represented as integers. Resources are positive, literals are negative. The value 0 is the "null node", and can be used as a wildcard when matching nodes or triples. Triples are represented as three-element tuples (duh!).
module piglet
The module implements only one function:
piglet.open(filename)
- This method creates/opens a triplestore and stores it in the specified file. This methods returns an instance of the class piglet.DB
class piglet.DB
The class DB implements the following methods:
close()
- Closes the triplestore. Returns True if successful.
query(subject, predicate, object[, source])
Matches (and returns, as a list) triples that match the given subject, predicate and object. Wildcards are allowed.
sources(subject, predicate, object)
- Returns (as a list) the "sources" from which corresponding matching triples have been added into the triplestore. Wildcards are allowed.
add(subject, predicate, object, source[, temporary])
Asserts a triple from the given source. If temporary is True, the triple is asserted "temporarily" (i.e., for the duration that the triplestore is open); it defults to False. Returns True if successful.
delete(subject, predicate, object, source[, temporary])
Deletes triples from the given source. If temporary is True (it defaults to False), temporarily asserted triples are deleted. Wildcards are allowed; passing a wildcard for source deletes matching triples from all sources. Returns true if successful.
count(subject, predicate, object, source[, temporary])
- Returns the number of matching triples.
info(node)
- For resources, returns the URI of the node (or None for bnodes). For literals, returns a tuple of three elements: the literal contents, the literal's datatype, and the literal's xml:lang attribute.
node(uri)
Registers the given URI as a resource node in the triplestore, and returns the corresponding index. For URIs already registered merely returns the index assigned at the time of registration. For resources n with a URI, the operation db.node(db.info(n)) is idempotent.
literal(string[, datatype[, language]])
- Registers ("interns") a literal in the triplestore. Optionally it is possible to pass a datatype (as a node index) and the literal's xml:lang attribute (as a string). According to the RDF specification, the datatype and the language are mutually exclusive - while this is not enforced in Piglet, you should pass 0 as the datatype if specifying a language.
load(source[, append])
- Loads a source (specificed as a node whose URI is the URL of the source you want to load) and asserts its contents into the triplestore. Returns True if successful.
nodeToString(node)
- Returns the Wilbur printed representation of a node (as a string).
tripleToString(subject, predicate, object)
- Returns the Wilbur printed representation of a triple (as a string).
expand(qname)
Expands an XML QName into the corresponding URI, based on the prefix/namespace mappings established for the triplestore. Returns None when no expansion can be made.
reverseExpand(uri)
Finds an XML QName for a URI, based on the prefix/namespace mappings established for the triplestore. If no QName can be found, returns None.
addNamespace(prefix, uri)
- Adds a mapping for a namespace.
delNamespace(prefix)
- Deletes a mapping for a namespace.
match(pattern)
Matches literals (and node URIs) that begin with pattern (a string). Results are returned as a list.
module pigletserver
The module implements a simple HTTP server that can be used to call the Piglet API; it serves back the API call results directly (as JSON, using json-py as the translator). Technically, the server is implemented as the classs pigletserver.PigletServer and pigletserver.PigletServerHandler. As an example, to start the server for Piglet database file "test.data" on port 8081, use this:
python pigletserver.py test.data 8081
Once the server is running, you can call it with HTTP GET where the entire call is encoded in the URL. For example, to call piglet.DB.query(1, 2, 3) you use the following URL:
http://localhost:8081/query?s=1&p=2&o=3
