Wednesday, September 14, 2011

Python Shelving

I discovered the cool shelve module earlier. Sometimes it's nice to be able to store python objects using the pickle module. The shelve module builds on this by providing a dictionary that you can put objects into and access later. Check out this gist
def add(x, y):
return x + y
def subtract(x, y):
return x - y
import shelve
d = shelve.open('shelve_state.dat')
d['add'] = add
d['subtract'] = subtract
func = d['add']
print func(3,5)
func = d['subtract']
print func(5, 1)
d.close
view raw shelving.py hosted with ❤ by GitHub

No comments:

Post a Comment