views:

54

answers:

3

HI,

I am implementing some unix userland tool, that needs to store hash on the disk. The hash will be read every run of a program, pretty frequently. To give you better insight, the hash needs to store "name:path" values only.

I looked at bdbd module for python, but I can see it will be deprecated in Python 3. Also saw Pickle.

Im not a Py Guy, so what is the efficient way for hash serialization and frequent open/read/close operations.

+3  A: 

I would start with the shelve module and see if that isn't too slow. It does exactly what you want.

import shelve

d = shelve.open('filename')

d['name'] = 'path'

d.close()

or to read from it

d = shelve.open('filename')

d = hash['name']

It's essentially a wrapper around pickle that provides a dictionary abstraction.

aaronasterling
+1 Cool, never heard of that module before. -0.1 for shadowing `hash`, though. :o) ... ok, that's been removed now.
jellybean
@jellybean, something looked wrong to me. changed it to `d` to reflect the fact that it's essentially a dict. good looking out.
aaronasterling
A: 

I'd use pickle and see if it's fast enough for your needs.

Russell Borogove
A: 

I would suggest you use pickle / shelve for serialization of data.

pyfunc