From d28821e901bd4c997cee5e02d9447f1a840f891a Mon Sep 17 00:00:00 2001 From: Lu Pan Date: Mon, 9 Feb 2015 13:34:37 -0800 Subject: [PATCH 1/3] Raise :class:`KeyError` if key doesn't exist in cache * Instead of returning '-1', which can be the actual value being stored, raises :class:`KeyError` when key doesn't exist in cache. * Added docstrings for :class:`LRUCache` --- boxsdk/object/events.py | 4 +++- boxsdk/util/lru_cache.py | 38 +++++++++++++++++++++++++++----- test/unit/util/test_lru_cache.py | 9 +++++--- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/boxsdk/object/events.py b/boxsdk/object/events.py index 09af502f1..8a014cd77 100644 --- a/boxsdk/object/events.py +++ b/boxsdk/object/events.py @@ -130,7 +130,9 @@ def generate_events_with_long_polling(self, stream_position=None): if message == 'new_change': next_stream_position = stream_position for event, next_stream_position in self._get_all_events_since(stream_position): - if event_ids.get(event['event_id']) == -1: + try: + event_ids.get(event['event_id']) + except KeyError: yield event event_ids.set(event['event_id']) stream_position = next_stream_position diff --git a/boxsdk/util/lru_cache.py b/boxsdk/util/lru_cache.py index a0e4d3e14..bc13e556b 100644 --- a/boxsdk/util/lru_cache.py +++ b/boxsdk/util/lru_cache.py @@ -6,19 +6,45 @@ class LRUCache(object): def __init__(self, capacity=512): + """ + :param capacity: + The Maximum number of key-value pairs can be cached. + :type capacity: + `int` + """ super(LRUCache, self).__init__() self.capacity = capacity self.cache = OrderedDict() def get(self, key): - try: - value = self.cache.pop(key) - self.cache[key] = value - return value - except KeyError: - return -1 + """ + Lookup the value in cache using the associated key. Returns the value if found. + Raises :class:`KeyError` otherwise. + :param key: + The key used to lookup the cache. + :type key: + `unicode` + :return: + The value associated with the key if exists. + :raises: + :class:`KeyError` if the key doesn't exist. + """ + value = self.cache.pop(key) + self.cache[key] = value + return value def set(self, key, value=None): + """ + Store the key-value pair to cache. + :param key: + The key associated with the value to be stored. It's used to lookup the cache. + :type key: + `unicode` + :param value: + The value to be stored. + :type value: + varies + """ try: self.cache.pop(key) except KeyError: diff --git a/test/unit/util/test_lru_cache.py b/test/unit/util/test_lru_cache.py index 1f46e3ac4..cae2edf44 100644 --- a/test/unit/util/test_lru_cache.py +++ b/test/unit/util/test_lru_cache.py @@ -18,7 +18,8 @@ def keys(): def test_lru_cache_returns_minus_one_for_missing_key(lru_cache, keys): # pylint:disable=redefined-outer-name for key in keys: - assert lru_cache.get(key) is -1 + with pytest.raises(KeyError): + lru_cache.get(key) def test_lru_cache_returns_none_for_existing_key(lru_cache, keys): @@ -34,9 +35,11 @@ def test_lru_cache_ejects_least_recently_used_key(lru_cache, keys): for key in keys: lru_cache.set(key) lru_cache.set('another key') - assert lru_cache.get(keys[0]) is -1 + with pytest.raises(KeyError): + lru_cache.get(keys[0]) assert lru_cache.get('another key') is None for key in keys[1:]: assert lru_cache.get(key) is None lru_cache.set('yet another key') - assert lru_cache.get('another key') is -1 + with pytest.raises(KeyError): + lru_cache.get('another key') From 2574e8ac43f5d520f6da5509dcba38eafe8bcf9b Mon Sep 17 00:00:00 2001 From: Lu Pan Date: Mon, 9 Feb 2015 13:58:46 -0800 Subject: [PATCH 2/3] Added blank lines before param lists so Sphinx can parse correctly --- boxsdk/util/lru_cache.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/boxsdk/util/lru_cache.py b/boxsdk/util/lru_cache.py index bc13e556b..1f59e7eb3 100644 --- a/boxsdk/util/lru_cache.py +++ b/boxsdk/util/lru_cache.py @@ -20,6 +20,7 @@ def get(self, key): """ Lookup the value in cache using the associated key. Returns the value if found. Raises :class:`KeyError` otherwise. + :param key: The key used to lookup the cache. :type key: @@ -36,6 +37,7 @@ def get(self, key): def set(self, key, value=None): """ Store the key-value pair to cache. + :param key: The key associated with the value to be stored. It's used to lookup the cache. :type key: From 2fffb54a28c7b09d37518efa79a179736d7c60f4 Mon Sep 17 00:00:00 2001 From: Lu Pan Date: Mon, 9 Feb 2015 15:17:14 -0800 Subject: [PATCH 3/3] Fixed typo --- boxsdk/util/lru_cache.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/boxsdk/util/lru_cache.py b/boxsdk/util/lru_cache.py index 1f59e7eb3..ee13942ee 100644 --- a/boxsdk/util/lru_cache.py +++ b/boxsdk/util/lru_cache.py @@ -18,11 +18,11 @@ def __init__(self, capacity=512): def get(self, key): """ - Lookup the value in cache using the associated key. Returns the value if found. + Look up the value in cache using the associated key. Returns the value if found. Raises :class:`KeyError` otherwise. :param key: - The key used to lookup the cache. + The key used to look up the cache. :type key: `unicode` :return: @@ -39,7 +39,7 @@ def set(self, key, value=None): Store the key-value pair to cache. :param key: - The key associated with the value to be stored. It's used to lookup the cache. + The key associated with the value to be stored. It's used to look up the cache. :type key: `unicode` :param value: