gpt4 book ai didi

python - 搁置模块有问题?

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:06 24 4
gpt4 key购买 nike

使用搁置模块给了我一些令人惊讶的行为。 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 library reference :

...The database is also (unfortunately) subject to the limitations of dbm, if it is used — this means that (the pickled representation of) the objects stored in the database should be fairly small...

这正确地重现了“错误”:

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),而是将计算结果存储在一个 shelf 中。

关于python - 搁置模块有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1326459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com