How to fill a shelf with an existing dictionary

Let's say that I have a large, 100 megabytes, dictionary that I want to make on a shelf on the disk. I use pypar to use MPI to create cleared bits of the main list. What is 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.
+5
source share
1 answer

Here you put a simple dictionary and make it accessible with the 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 should be legible, as for any that should be delayed.

, .. myDict, , update :

myShelvedDict.update(myDict)

shelve dict.

+6

All Articles