This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Python bindings for the sophia database.

Project Description
<a href=""><img src="" width="215px" height="95px" /></a>

`sophy`, fast Python bindings for [Sophia Database](, v2.1.


* Append-only MVCC database
* ACID transactions
* Consistent cursors
* Compression
* Multi-part keys
* Ordered key/value store
* Range searches
* Read-only views of point-in-time
* Prefix searches
* Python 2.x and 3.x


* Not tested on Windoze.

The source for `sophy` is [hosted on GitHub](

If you encounter any bugs in the library, please [open an issue](, including a description of the bug and any related traceback.

## Installation

The [sophia]( sources are bundled with the `sophy` source code, so the only thing you need to install is [Cython]( You can install from [GitHub]( or from [PyPI](

Pip instructions:

$ pip install Cython sophy

Git instructions:

$ pip install Cython
$ git clone
$ cd sophy
$ python build
$ python install

## Usage

Sophy is very simple to use. It acts primarly like a Python `dict` object, but in addition to normal dictionary operations, you can read slices of data that are returned efficiently using cursors. Similarly, bulk writes using `update()` use an efficient, atomic batch operation.

To begin, instantiate your Sophia database. Multiple databases can exist under the same path:

>>> from sophy import connect
>>> db = connect('data-dir', 'db-name')

We can set values individually or in groups:

>>> db['k1'] = 'v1'
>>> db.update(k2='v2', k3='v3', k4='v4') # Efficient, atomic.
>>> with db.transaction() as txn: # Same as .update()
... txn['k1'] = 'v1-e'
... txn['k5'] = 'v5'

We can read values individually or in groups. When requesting a slice, the third parameter (`step`) is used to indicate the results should be returned in reverse. Alternatively, if the first key is higher than the second key in a slice, `sophy` will interpret that as ordered in reverse.

>>> db['k1']
>>> [item for item in db['k2': 'k444']]
[('k2', 'v2'), ('k3', 'v3'), ('k4', 'v4')]

>>> list(db['k3':'k1']) # Results are returned in reverse.
[('k3', 'v3'), ('k2', 'v2'), ('k1', 'v1-e')]

>>> list(db[:'k3']) # All items from lowest key up to 'k3'.
[('k1', 'v1-e'), ('k2', 'v2'), ('k3', 'v3')]

>>> list(db[:'k3':True]) # Same as above, but ordered in reverse.
[('k3', 'v3'), ('k2', 'v2'), ('k1', 'v1-e')]

>>> list(db['k3':]) # All items from k3 to highest key.
[('k3', 'v3'), ('k4', 'v4'), ('k5', 'v5')]

Values can also be deleted singly or in groups. To delete multiple items atomically use the `transaction()` method.

>>> del db['k3']
>>> db['k3'] # 'k3' no longer exists.
Traceback (most recent call last):
KeyError: 'k3'

>>> with db.transaction() as txn:
... del txn['k2']
... del txn['k5']
... del txn['kxx'] # No error raised.
>>> list(db)
[('k1', 'v1-e'), ('k4', 'v4')]

### Transactions

>>> with db.transaction() as txn:
... txn['k2'] = 'v2-e'
... txn['k1'] = 'v1-e2'
>>> list(db[::True])
[('k4', 'v4'), ('k2', 'v2-e'), ('k1', 'v1-e2')]

You can call `commit()` or `rollback()` inside the transaction block itself:

>>> with db.transaction() as txn:
... txn['k1'] = 'whoops'
... txn.rollback()
... txn['k2'] = 'v2-e2'
>>> list(db.items())
[('k1', 'v1-e2'), ('k2', 'v2-e2'), ('k4', 'v4')]

If an exception occurs in the wrapped block, the transaction will automatically be rolled back.

### Cursors

Sophia is an ordered key/value store, so cursors will by default iterate through the keyspace in ascending order. To iterate in descending order, you can specify this using the slicing technique described above. For finer-grained control, however, you can use the `cursor()` method.

The `Database.cursor()` method supports a number of interesting parameters:

* `order`: either `'>='` (default), `'<='` (reverse), `'>'` (ascending not including endpoint), `'<'` (reverse, not including endpoint).
* `key`: seek to this key before beginning to iterate.
* `prefix`: perform a prefix search.
* `keys`: include the database key while iterating (default `True`).
* `values`: include the database value while iterating (default `True`).

To perform a prefix search, for example, you might do something like:

>>> db.update(aa='foo', abc='bar', az='baze', baz='nugget')
>>> for item in db.cursor(prefix='a'):
... print item
('aa', 'foo')
('abc', 'bar')
('az', 'baze')

### Configuration

Sophia supports a huge number of [configuration options](, most of which are exposed as simple properties on the `Sophia` or `Database` objects. For example, to configure a `Sophia` database to use mmap and compression:

>>> env = Sophia('my/data-dir', log_path='my/log-dir', threads=4)
>>> db = env.database('my-db', mmap=True, sync=True, compression='zstd')

You can also force checkpointing, garbage-collection, and other things using simple methods:

>>> db.checkpoint()
>>> db.gc()

Some properties are read-only:

>>> db.index_count
>>> len(db)
>>> db.status
>>> db.memory_used

Take a look at the [configuration docs]( for more details.

### Views


### Multi-part keys

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
sophy-0.1.6.tar.gz (334.2 kB) Copy SHA256 Checksum SHA256 Source Feb 3, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting