Lets say I have a large, 100s of megabytes, dictionary that I want to make into an on disk shelve. I'm using pypar to utilize MPI to generate cleaned bits of a master list. What's the best way to achieve this? Example:
# much earlier
masterDict = shelve.open( 'aShelveFile' )
# .. . . . .
# then we work out which parts of masterDict to keep
# and we put into aCleanDict
# then use mpi pypar to send the cleaned bits to the master rank
if pypar.rank() == 0:
tempdict = {}
for p in range(1,pypar.size()):
tempdict.append(pypar.receive(p))
for l1 in tempdict:
for l2 in l1:
realDict.append(l2)
for p in range(1,pypar.size()):
pypar.send(realDict,p)
# now realDict has all the cleaned bits
# which we send to the other hosts
else:
pypar.send(aCleanDict, 0 )
aCleanDict = pypar.receive( 0 )
# now to replace masterDict with aCleanDict
# note: cannot send shelve dictonaries using pypar
# insert stackover flow code here.
Here you shelve a simple dictionary and make it accessibly via key myDict
:
import shelve
myDict = {"a" : 1, "b" : 2}
myShelvedDict = shelve.open("my_shelved_dictionary.db")
myShelvedDict["myDict"] = myDict
Note that the contents of the dictionary must be pickleable, as for anything that is to be shelved.
If you want to replicate the structure of the dictionary in the shelve, i.e. not have a myDict
key but the keys of the dictionary directly as keys of the shelf, you can use the update
method of shelve:
myShelvedDict.update(myDict)
The shelve
interface has a large overlap with the dict
one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With