質問
Sheleveモジュールを使用すると、驚くべき振る舞いが与えられました。 keys()、iter()、およびiteritems()棚のすべてのエントリを返さないでください!これがコードです:
cache = shelve.open('my.cache')
# ...
cache[url] = (datetime.datetime.today(), value)
後で:
cache = shelve.open('my.cache')
urls = ['accounts_with_transactions.xml', 'targets.xml', 'profile.xml']
try:
print list(cache.keys()) # doesn't return all the keys!
print [url for url in urls if cache.has_key(url)]
print list(cache.keys())
finally:
cache.close()
そして、これが出力です:
['targets.xml']
['accounts_with_transactions.xml', 'targets.xml']
['targets.xml', 'accounts_with_transactions.xml']
以前に誰かがこれに出くわしましたか? アプリオリ?
解決
による Pythonライブラリリファレンス:
...データベースは(残念ながら)DBMの制限の対象となります。これは、データベースに保存されているオブジェクトがかなり小さいことを意味します。
これにより、「バグ」が正しく再現されます。
import shelve
a = 'trxns.xml'
b = 'foobar.xml'
c = 'profile.xml'
urls = [a, b, c]
cache = shelve.open('my.cache', 'c')
try:
cache[a] = a*1000
cache[b] = b*10000
finally:
cache.close()
cache = shelve.open('my.cache', 'c')
try:
print cache.keys()
print [url for url in urls if cache.has_key(url)]
print cache.keys()
finally:
cache.close()
出力で:
[]
['trxns.xml', 'foobar.xml']
['foobar.xml', 'trxns.xml']
したがって、答えは、生のXMLのような大きなものを保存しないことですが、棚にある計算結果です。
他のヒント
あなたの例を見て、私の最初の考えはそれです cache.has_key()
副作用があります。つまり、この呼び出しはキャッシュにキーを追加します。何のために手に入れますか
print cache.has_key('xxx')
print list(cache.keys())
所属していません StackOverflow