2011-07-10 8 views
5

ディスクシェルフ上に作成したい辞書を100メガバイトという大きなサイズにします。私はMPIを使用してマスターリストのクリーンビットを生成するためにpyparを使用しています。これを達成する最良の方法は何ですか?例:ここでは既存の辞書を使用してシェルフを作成する方法

# 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. 
+2

それを棚上げするだけで問題はありますか? – juanchopanza

+0

既存の辞書をどのように棚上げしますか? – Martlark

+0

私は簡単な例を加えました。これがあなたが必要としているものかどうかはわかりません。そうでない場合は、少し質問を明確にしたいかもしれません。 – juanchopanza

答えて

6

あなたは、簡単な辞書を棚上げし、キーmyDict経由でアクセス可能にそれを作る:辞書の内容が棚上げされるものについては、pickleableでなければならないこと

import shelve 
myDict = {"a" : 1, "b" : 2} 
myShelvedDict = shelve.open("my_shelved_dictionary.db") 
myShelvedDict["myDict"] = myDict 

注意を。

myShelvedDict.update(myDict) 
:あなたは myDictキーが、直接、棚のキーとして辞書のキーを持っていない、すなわち、棚にある辞書の構造を複製したい場合は

は、あなたは棚のupdate方法を使用することができます

shelveインターフェイスは、dictと大きく重なっています。

+0

私がしていることをより明確にするためのコードをいくつか追加しました。 – Martlark