From 4eae44bcd8552a0580ec27139536f9f8f98f905f Mon Sep 17 00:00:00 2001 From: Anton Hulikau Date: Tue, 3 Apr 2018 01:53:36 +0300 Subject: [PATCH] Update lru_cache.py [] operator can raise KeyError in dict. --- solutions/object_oriented_design/lru_cache/lru_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/object_oriented_design/lru_cache/lru_cache.py b/solutions/object_oriented_design/lru_cache/lru_cache.py index 75fe44d4505..acee46516eb 100644 --- a/solutions/object_oriented_design/lru_cache/lru_cache.py +++ b/solutions/object_oriented_design/lru_cache/lru_cache.py @@ -34,7 +34,7 @@ def get(self, query): Accessing a node updates its position to the front of the LRU list. """ - node = self.lookup[query] + node = self.lookup.get(query) if node is None: return None self.linked_list.move_to_front(node) @@ -47,7 +47,7 @@ def set(self, results, query): If the entry is new and the cache is at capacity, removes the oldest entry before the new entry is added. """ - node = self.lookup[query] + node = self.lookup.get(query) if node is not None: # Key exists in cache, update the value node.results = results