IPython Documentation

Table Of Contents

Previous topic

utils.path

Next topic

utils.pickleutil

This Page

utils.pickleshare

Module: utils.pickleshare

Inheritance diagram for IPython.utils.pickleshare:

PickleShare - a small ‘shelve’ like datastore with concurrency support

Like shelve, a PickleShareDB object acts like a normal dictionary. Unlike shelve, many processes can access the database simultaneously. Changing a value in database is immediately visible to other processes accessing the same database.

Concurrency is possible because the values are stored in separate files. Hence the “database” is a directory where all files are governed by PickleShare.

Example usage:

from pickleshare import *
db = PickleShareDB('~/testpickleshare')
db.clear()
print "Should be empty:",db.items()
db['hello'] = 15
db['aku ankka'] = [1,2,313]
db['paths/are/ok/key'] = [1,(5,46)]
print db.keys()
del db['aku ankka']

This module is certainly not ZODB, but can be used for low-load (non-mission-critical) situations where tiny code size trumps the advanced features of a “real” object database.

Installation guide: easy_install pickleshare

Author: Ville Vainio <vivainio@gmail.com> License: MIT open source license.

Classes

PickleShareDB

class IPython.utils.pickleshare.PickleShareDB(root)

Bases: _abcoll.MutableMapping

The main ‘connection’ object for PickleShare database

__init__(root)

Return a db object that will manage the specied directory

clear() → None. Remove all items from D.
get(k[, d]) → D[k] if k in D, else d. d defaults to None.

Get a convenient link for accessing items

hcompress(hashroot)

Compress category ‘hashroot’, so hset is fast again

hget will fail if fast_only is True for compressed items (that were hset before hcompress).

hdict(hashroot)

Get all data contained in hashed category ‘hashroot’ as dict

hget(hashroot, key, default=<object object at 0x8fda5190>, fast_only=True)

hashed get

hset(hashroot, key, value)

hashed set

items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys(globpat=None)

All keys in DB, or all keys matching a glob

pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
uncache(*items)

Removes all, or specified items from cache

Use this after reading a large amount of large objects to free up memory, when you won’t be needing the objects for a while.

update([E], **F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() → list of D's values
waitget(key, maxwaittime=60)

Wait (poll) for a key to get a value

Will wait for maxwaittime seconds before raising a KeyError. The call exits normally if the key field in db gets a value within the timeout period.

Use this for synchronizing different processes or for ensuring that an unfortunately timed “db[‘key’] = newvalue” operation in another process (which causes all ‘get’ operation to cause a KeyError for the duration of pickling) won’t screw up your program logic.

Functions

IPython.utils.pickleshare.gethashfile(key)
IPython.utils.pickleshare.main()
IPython.utils.pickleshare.stress()
IPython.utils.pickleshare.test()