From: Mike Bayer Date: Fri, 20 Dec 2019 17:49:13 +0000 (-0500) Subject: Implement random_choices for Python 2 X-Git-Tag: rel_1_4_0b1~587 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c54b27ab123a4bf29ba7bc76924e188e2bc88e9f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Implement random_choices for Python 2 Apparently py2k has no random.choices, so make a quick one for the tests that use it. Change-Id: Iadc3442b35f400b5bab0f711b7d3ede5dbc28f52 --- diff --git a/lib/sqlalchemy/testing/util.py b/lib/sqlalchemy/testing/util.py index dbe6a383de..5244d0d377 100644 --- a/lib/sqlalchemy/testing/util.py +++ b/lib/sqlalchemy/testing/util.py @@ -66,6 +66,21 @@ def picklers(): yield pickle_.loads, lambda d: pickle_.dumps(d, protocol) +if py2k: + + def random_choices(population, k=1): + pop = list(population) + # lame but works :) + random.shuffle(pop) + return pop[0:k] + + +else: + + def random_choices(population, k=1): + return random.choices(population, k=k) + + def round_decimal(value, prec): if isinstance(value, float): return round(value, prec) diff --git a/test/sql/test_compare.py b/test/sql/test_compare.py index f8fc43ba54..6a2d47c20b 100644 --- a/test/sql/test_compare.py +++ b/test/sql/test_compare.py @@ -59,6 +59,7 @@ from sqlalchemy.testing import is_false from sqlalchemy.testing import is_not_ from sqlalchemy.testing import is_true from sqlalchemy.testing import ne_ +from sqlalchemy.testing.util import random_choices from sqlalchemy.util import class_hierarchy @@ -392,13 +393,13 @@ class CoreFixtures(object): lambda: ( # same number of params each time, so compare for IN # with legacy behavior of bind for each value works - column("x").in_(random.choices(range(10), k=3)), + column("x").in_(random_choices(range(10), k=3)), # expanding IN places the whole list into a single parameter # so it can be of arbitrary length as well column("x").in_( bindparam( "q", - random.choices(range(10), k=random.randint(0, 7)), + random_choices(range(10), k=random.randint(0, 7)), expanding=True, ) ),