]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
a RandomSet implementation useful for swapping into topological
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2010 17:07:17 +0000 (13:07 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2010 17:07:17 +0000 (13:07 -0400)
lib/sqlalchemy/test/util.py
lib/sqlalchemy/topological.py

index 8a3a0e7452a25c18ac3d294c00acb44bd9728fb4..cd73b44d07917cd1013a03f0b6c144fb95c966c1 100644 (file)
@@ -2,6 +2,7 @@ from sqlalchemy.util import jython, function_named
 
 import gc
 import time
+import random
 
 if jython:
     def gc_collect(*args):
@@ -50,4 +51,17 @@ def round_decimal(value, prec):
     # can also use shift() here but that is 2.6 only
     return (value * decimal.Decimal("1" + "0" * prec)).to_integral(decimal.ROUND_FLOOR) / \
                         pow(10, prec)
-    
\ No newline at end of file
+    
+class RandomSet(set):
+    def __iter__(self):
+        l = list(set.__iter__(self))
+        random.shuffle(l)
+        return iter(l)
+    
+    def pop(self):
+        index = random.randint(0, len(self) - 1)
+        item = list(set.__iter__(self))[index]
+        self.remove(item)
+        return item
+        
+        
\ No newline at end of file
index 603bb055b5a1a92270b2624fd14dd2ae81541f70..07b1ba03e4385a137d762d734bed9049f9bec905 100644 (file)
@@ -9,6 +9,7 @@
 from sqlalchemy.exc import CircularDependencyError
 from sqlalchemy import util
 
+
 __all__ = ['sort']
 
 class _EdgeCollection(object):