Implemented more robust delayed registry iteration. Modules declaring new searches...
[philo.git] / philo / contrib / sobol / utils.py
index 3c5e537..50d2113 100644 (file)
@@ -1,8 +1,9 @@
+from hashlib import sha1
+
 from django.conf import settings
 from django.http import QueryDict
 from django.utils.encoding import smart_str
 from django.utils.http import urlquote_plus, urlquote
-from hashlib import sha1
 
 
 SEARCH_ARG_GET_KEY = 'q'
@@ -29,4 +30,25 @@ def make_tracking_querydict(search_arg, url):
                SEARCH_ARG_GET_KEY, urlquote_plus(search_arg),
                URL_REDIRECT_GET_KEY, urlquote(url),
                HASH_REDIRECT_GET_KEY, make_redirect_hash(search_arg, url))
-       )
\ No newline at end of file
+       )
+
+
+class RegistryIterator(object):
+       def __init__(self, registry, iterattr='__iter__', transform=lambda x:x):
+               if not hasattr(registry, iterattr):
+                       raise AttributeError("Registry has no attribute %s" % iterattr)
+               self.registry = registry
+               self.iterattr = iterattr
+               self.transform = transform
+       
+       def __iter__(self):
+               return self
+       
+       def next(self):
+               if not hasattr(self, '_iter'):
+                       self._iter = getattr(self.registry, self.iterattr)()
+               
+               return self.transform(self._iter.next())
+       
+       def copy(self):
+               return self.__class__(self.registry, self.iterattr, self.transform)
\ No newline at end of file