changeset 2:f9ee6f8f7021

added whoisi_cache.py
author Atul Varma <varmaa@toolness.com>
date Thu, 31 Dec 2009 12:30:18 -0800
parents 8721d49f46be
children 8c35fbdf5f43
files test.py whoisi_cache.py
diffstat 2 files changed, 53 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/test.py	Thu Dec 31 11:38:59 2009 -0800
+++ b/test.py	Thu Dec 31 12:30:18 2009 -0800
@@ -1,5 +1,26 @@
 import unittest
 
-class Tests(unittest.TestCase):
-    def testBasic(self):
-        raise NotImplementedError()
+import whoisi_cache
+
+class FakeWhoisiServer(object):
+    def __init__(self, people):
+        self._people = [None]
+        self._people.extend(people)
+
+    def get_max_person_id(self, app):
+        return len(self._people) - 1
+
+    def get_people(self, app, first, last):
+        if first == 0:
+            raise ValueError('do not ask for person id 0')
+        if last > self.get_max_person_id(app):
+            raise ValueError('bad last id')
+        return tuple(self._people[first:last+1])
+
+class WhoisiCacheTests(unittest.TestCase):
+    def testUpdateWorks(self):
+        people = ["a", "b", "c"]
+        server = FakeWhoisiServer(people)
+        cache = whoisi_cache.WhoisiCache(server, batch_size=2)
+        cache.update()
+        self.assertEqual(people, cache.people)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/whoisi_cache.py	Thu Dec 31 12:30:18 2009 -0800
@@ -0,0 +1,29 @@
+MAX_PEOPLE_REQ_SIZE = 100
+
+def split_seq(seq, size):
+    """
+    Split up the given sequence into pieces of the given size.
+
+    Taken from http://code.activestate.com/recipes/425044/.
+    """
+
+    return [seq[i:i+size] for i in range(0, len(seq), size)]
+
+class WhoisiCache(object):
+    APP_NAME = "whoisi-cache"
+
+    def __init__(self, server, batch_size=MAX_PEOPLE_REQ_SIZE):
+        self.server = server
+        self.batch_size = batch_size
+        self.people = []
+
+    def update(self):
+        pid = self.server.get_max_person_id(app=self.APP_NAME)
+        interval = range(len(self.people) + 1, pid + 1)
+        if interval:
+            subintervals = split_seq(interval, self.batch_size)
+            for subinterval in subintervals:
+                people = self.server.get_people(app=self.APP_NAME,
+                                                first=subinterval[0],
+                                                last=subinterval[-1])
+                self.people.extend(people)