Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to store dictionary (hash) in file with python?

I'm implementing a Unix userland tool that needs to store a hash on the disk. The hash will be read every run of the program, pretty frequently. The hash needs to store "name:path" values only.

I looked at the bsddb standard library module for python, but I can see it will be deprecated in Python 3. I also saw the pickle standard library module.

I'm not a python guy, so what is the efficient way for hash serialization and frequent open/read/close operations?

like image 588
Darek Avatar asked Sep 16 '10 07:09

Darek


People also ask

Can a dictionary be hashed in Python?

In Python, the Dictionary data types represent the implementation of hash tables. The Keys in the dictionary satisfy the following requirements. The keys of the dictionary are hashable i.e. the are generated by hashing function which generates unique result for each unique value supplied to the hash function.


1 Answers

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.

like image 148
aaronasterling Avatar answered Sep 23 '22 15:09

aaronasterling