Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install sparrow

How to install sparrow

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install sparrow
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.0b4 Available View build log
Windows (64-bit)
1.0b4 Available View build log
Mac OS X (10.5+)
1.0b4 Available View build log
Linux (32-bit)
1.0b4 Available View build log
Linux (64-bit)
1.0b4 Available View build log
 
License
BSD
Lastest release
version 1.0b4 on Jan 5th, 2011

Sparrow

Introduction

Sparrow is a library that provides a high-level API for RDF Databases. Any database the provides support for SPARQL Queries and has a triplestore that can handle contexts can be used as a backend.

The goal of Sparrow is to make sure all these different backends act the same, making it possible to change RDF Database backends without having to change your code.

At the moment there is support for the following backends:

The API provides support for the following basic functions:

  • Parsing RDF statements from different formats into a contextual database
  • Serializing the RDF statements from a specific context of a database
  • Removing statements from a specific context of a database
  • Performing SPARQL Queries

Sparrow does not provide a Graph API, but it can parse and serialize python dictionary objects. This uses the same format as the JSON serialization. Each TripleStore backend provides the following formats:

  • RDFXML
  • NTriples
  • Turtle
  • JSON
  • Python dictionaries
Installation

Sparrow comes with buildout profiles for several databases. These profiles will install and setup the different backends for you. You don't have to use buildout, but I would recommend it.

To install type:

> python2.5 bootstrap.py
> ./bin/buildout

This will create some scripts in the bin folder like a testrunner and (depending on which profile you choose) scripts for configuring and starting the different backends.

Usage

Normally, you will only need to import the base sparrow module

>>> import sparrow

Most of the database backends will not work out of the box. Since the RDFLib backend is written in python and packaged on pypi, it is always available, and installed with Sparrow.

Let's create an in memory rdflib database

>>> db = sparrow.database('rdflib', 'memory')
>>> db
<sparrow.rdflib_backend.RDFLibTripleStore ...>

Let's add some triples to the database, we will use turtle syntax for this. We'll make some example statements where we will state that john is a person, and that his firstname is "John".

>>> data = """@prefix ex: <http://example.org#> .
... ex:john a ex:Person; ex:name "John" ."""

Now we can add this to the database. We will need to tell the database in which context to store the data. The data itself can be either a file or http based URI, a string of data, or a file-like object.

So, let's add this to the persons context.

>>> db.add_turtle(StringIO(data), 'persons')

We can now ask the database, which contexts it has:

>>> db.contexts()
[u'persons']

You can store data in as many different contexts as you like, or put everything in a single context.

Lets do a simple SPARQL query on the database

>>> result = db.select('SELECT ?x {?x <http://example.org#name> "John".}')

We can get the results as a list of dictionaries. This follows the SPARQL JSON result format.

>>> result
[{u'x': {'type': u'uri', 'value': u'http://example.org#john'}}]

Besides querying, we can also get the data back from the database in any of the supported formats. We specify which format we want, and which context to use.

>>> db.get_ntriples('persons').read()
'<http://example.org#john> ...'

If the database backend supports it, you can ask how many triples are in a context.

>>> db.count('persons')
2

If you want to remove triples, you will need to supply data describing which triples to remove.

>>> data = '<http://example.org#john> a <http://example.org#Person>.'
>>> db.remove_turtle(data, 'persons')
>>> db.count('persons')
1

You can also remove all triples in a context

>>> db.clear('persons')
>>> db.count('persons')
0

Since the 'persons' context is now empty, it is also removed.

>>> db.contexts()
[]

Changelog for Sparrow

Sparrow 1.0b4 (2009-07-20)
  • Picked better default profile
Sparrow 1.0b3 (2009-07-20)
  • Fixed import errors in redland and allegro backend
  • API Changes: removed format method, all stores are supposed to

System Message: WARNING/2 (<string>, line 157)

Bullet list ends without a blank line; unexpected unindent.

provide 'ntriples', 'rdfxml' and 'turtle' serializations - API Changes: renamed IDatabase to ITripleStore, added ISPARQLEndpoint interface, added methods for different formats. - Added json and python dict serialization and parser formats for all backends - Added configure.zcml with utility registrations - Fixed allegro query tests

Sparrow 1.0b2 (2009-07-13)
  • Added Allegro backend
  • More unittests
  • Added Allegro graph buildout profile
Sparrow 1.0b1 (2009-07-12)
  • Refactored Redland/LibRDF, Sesame backends from Metro
  • Added RDFLib backend
  • Added Sesame buildout profile, configure and start scripts
  • Added basic test coverage

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.