2017-07-19 146 views
0

我正在使用緩存工具庫中的LRUCache,但是當我試圖追加我得到的錯誤'字典'對象沒有屬性'追加' 雖然我明白什麼是錯誤,我不能似乎想出任何方法來解決它,有人可以幫忙嗎? 這裏是一個小小的代碼。緩存工具的LRU緩存問題

GivenQuestionsCache=LRUCache(maxsize=100,missing=getGivenQuestions) 
now GivenQuestionsCache[1] gives 
{1: [[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None]]} 

,我試圖做

GivenQuestionsCache[1].append([10,None]) 

則拋出錯誤。有沒有其他方法可以實現這一目標? 我想我的緩存成爲

{1: [[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None],[10,None]]} 
+0

「getGivenQuestions」是否返回字典?如果是 - 那麼GivenQuestionsCache將使用缺少的工廠而不是此列表。 –

+0

是的,它確實返回字典。對不起,什麼缺少工廠? – xmen

+0

「失蹤」的屬性是當你有一個使用工廠緩存未命中 GivenQuestionsCache [a_key_does_not_exists] == getGivenQuestions() 現在,你需要找出爲什麼它沒有在緩存中的「1」 –

回答

1

我測試你的代碼,它的工作原理:

from cachetools import LRUCache 
GivenQuestionsCache=LRUCache(maxsize=100,missing=lambda _: dict()) 
GivenQuestionsCache[1] = [[211736, None], [211736, 'a'], [207113, 'a'], [219556, None], [207095, None], [89027, None], [89027, None]] 
GivenQuestionsCache[1].append([10,None]) 
print GivenQuestionsCache[1] 

回報

[[211736, None], 
[211736, 'a'], 
[207113, 'a'], 
[219556, None], 
[207095, None], 
[89027, None], 
[89027, None], 
[10, None]] 

GivenQuestionsCache[2].append([10,None])

將返回

AttributeError: 'dict' object has no attribute 'append' 

所以你需要檢查所有的代碼,可能可以修改GivenQuestionsCache。

+0

它不適用於我,您可以檢查它是否適用於python3 + – xmen

+0

print(GivenQuestionsCache .__ contains __(studentId)) GivenQuestionsCache。 __getitem __(studentId).append([int(questionId),None]) 我這樣做,第一行打印真實,但我仍然收到錯誤,沒有其他人修改緩存 – xmen

+0

hmm,在Python 3.4.3和python 2.7.6中測試 - 沒有問題 –