Shelve it with python

One of the little gems hidden in python standard library is shelve.

The shelve module can be used as a simple persistent storage option for Python objects when a relational database is overkill. The shelf is accessed by keys, just as with a dictionary. The values are pickled and written to a database created and managed by dbm.

import shelve

with shelve.open('test_shelf.db') as s:
    s['key1'] = {
        'int': 310,
        'float': 3.14.5,
        'string': 'Sample string data',
	'array': [[1,2,3],[4,5,6]],
    }

I mostly work with large simulation data and run simulation from python and these simulations take time to run sometimes days, so a simple persistent storage option provided by shelve is an intuitive way to restore my work.

An advantage of is we do not have to remember the order in which the objects are pickled, since shelve gives a dictionary-like object.

Here’s a sample code I use to store my long running results and latter to restore those values at a later time.

filename=r"flake_results.out"

my_shelf = shelve.open(filename, "n")

for key in ["stress", "strain", "plas", "creep","temp"]:
	try:	
		my_shelf[key]=globals()[key]
	except Exception:
		print ("Error shelving: {}".format(key))

my_shelf.close()

To restore

my_shelf = shelve.open(filename)
for key in my_shelf:
    globals()[key]=my_shelf[key]

my_shelf.close()

for more info: Visit This

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s