Commits

Joel Rivera committed 8e63681

Update rr_cache to be python3 compatible.

  • Participants
  • Parent commits 03186e6
  • Branches py3k

Comments (0)

Files changed (1)

CodernityDB/rr_cache.py

 import functools
 from random import choice
 
+import six
+
+if six.PY3:
+    xrange = range
 
 def cache1lvl(maxsize=100):
     def decorating_function(user_function):
             except KeyError:
                 if len(cache1lvl) == maxsize:
                     for i in xrange(maxsize // 10 or 1):
-                        del cache1lvl[choice(cache1lvl.keys())]
+                        del cache1lvl[choice(list(cache1lvl.keys()))]
                 cache1lvl[key] = user_function(key, *args, **kwargs)
                 result = cache1lvl[key]
 #                result = user_function(obj, key, *args, **kwargs)
                 if wrapper.cache_size == maxsize:
                     to_delete = maxsize // 10 or 1
                     for i in xrange(to_delete):
-                        key1 = choice(cache.keys())
-                        key2 = choice(cache[key1].keys())
+                        key1 = choice(list(cache.keys()))
+                        key2 = choice(list(cache[key1].keys()))
                         del cache[key1][key2]
                         if not cache[key1]:
                             del cache[key1]