From: Mike Bayer Date: Tue, 6 Apr 2010 17:07:17 +0000 (-0400) Subject: a RandomSet implementation useful for swapping into topological X-Git-Tag: rel_0_6_0~46 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=52c0f5691377e7846bf80ce6f25a4f3b11bf5a88;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git a RandomSet implementation useful for swapping into topological --- diff --git a/lib/sqlalchemy/test/util.py b/lib/sqlalchemy/test/util.py index 8a3a0e7452..cd73b44d07 100644 --- a/lib/sqlalchemy/test/util.py +++ b/lib/sqlalchemy/test/util.py @@ -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 diff --git a/lib/sqlalchemy/topological.py b/lib/sqlalchemy/topological.py index 603bb055b5..07b1ba03e4 100644 --- a/lib/sqlalchemy/topological.py +++ b/lib/sqlalchemy/topological.py @@ -9,6 +9,7 @@ from sqlalchemy.exc import CircularDependencyError from sqlalchemy import util + __all__ = ['sort'] class _EdgeCollection(object):