Repository: box/box-python-sdk
Pull request #4 (2015-02-09 21:47:49)

Improved the LRUCache class and added dostrings
I think it's wrong to return any value if the key doesn't exist in the cache since there's no good way to differentiate it from the actual value stored in cache.


Patch:
diff --git a/boxsdk/object/events.py b/boxsdk/object/events.py
index 09af502..8a014cd 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 a0e4d3e..ee13942 100644
--- a/boxsdk/util/lru_cache.py
+++ b/boxsdk/util/lru_cache.py
@@ -6,19 +6,47 @@
 
 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
+        """
+        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 look up 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 look up the cache.
+        :type key:
+            `unicode`
+        :param value:
+            The value to be stored.
+        :type value:
+            varies
+        """
         try:
             self.cache.pop(key)
         except KeyError:


Test patch:
diff --git a/test/unit/util/test_lru_cache.py b/test/unit/util/test_lru_cache.py
index 1f46e3a..cae2edf 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')