diff whoisi_cache.py @ 2:f9ee6f8f7021

added whoisi_cache.py
author Atul Varma <varmaa@toolness.com>
date Thu, 31 Dec 2009 12:30:18 -0800
parents
children 8c35fbdf5f43
line wrap: on
line diff
--- /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)