From 261b1f8af565ef8c0e30a6b1e6cd68769faff5f4 Mon Sep 17 00:00:00 2001 From: Richard Postelnik Date: Tue, 7 Mar 2017 00:27:26 -0500 Subject: [PATCH 1/6] add a function for lazily generating integers --- toolz/itertoolz.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 2462c5fb..e494c7f0 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -14,7 +14,7 @@ 'first', 'second', 'nth', 'last', 'get', 'concat', 'concatv', 'mapcat', 'cons', 'interpose', 'frequencies', 'reduceby', 'iterate', 'sliding_window', 'partition', 'partition_all', 'count', 'pluck', - 'join', 'tail', 'diff', 'topk', 'peek', 'random_sample') + 'join', 'tail', 'diff', 'topk', 'peek', 'random_sample', 'randint_range') def remove(predicate, seq): @@ -979,3 +979,26 @@ def random_sample(prob, seq, random_state=None): if not hasattr(random_state, 'random'): random_state = Random(random_state) return filter(lambda _: random_state.random() < prob, seq) + + +def randint_range(sample_size, min, max, random_state=None): + """ Generates a random sequence of integers from a specified range + + Returns a lazy iterator of random integers from a range. + + >>> list(randint_range(5, 100, 1000)) + [157, 196, 781, 882, 905] + >>> list(randint_range(5, 100, 1000)) + [234, 377, 501, 601, 885] + >>> list(randint_range(5, 100, 1000)) + [441, 540, 717, 736, 946] + """ + if not hasattr(random_state, 'random'): + random_state = Random(random_state) + population_size = max - min + for i in range(min, max): + if random_state.random() < sample_size / population_size: + yield i + sample_size -= 1 + population_size -= 1 + From 6b941fe9f5a5305d6b940cf83a20a5656bcaa2c5 Mon Sep 17 00:00:00 2001 From: Richard Postelnik Date: Tue, 7 Mar 2017 23:01:26 -0500 Subject: [PATCH 2/6] fix doctests --- toolz/itertoolz.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index e494c7f0..23fdc30a 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -986,12 +986,14 @@ def randint_range(sample_size, min, max, random_state=None): Returns a lazy iterator of random integers from a range. - >>> list(randint_range(5, 100, 1000)) + >>> list(randint_range(5, 100, 1000)) # doctest: +SKIP [157, 196, 781, 882, 905] - >>> list(randint_range(5, 100, 1000)) + >>> list(randint_range(5, 100, 1000)) # doctest: +SKIP [234, 377, 501, 601, 885] - >>> list(randint_range(5, 100, 1000)) - [441, 540, 717, 736, 946] + >>> list(randint_range(5, 100, 1000, random_state=2017)) + [270, 548, 595, 878, 999] + >>> list(randint_range(5, 100, 1000, random_state=2017)) + [270, 548, 595, 878, 999] """ if not hasattr(random_state, 'random'): random_state = Random(random_state) From acc0cee9d9b119d83f054a13536dff51cbd0d294 Mon Sep 17 00:00:00 2001 From: Richard Postelnik Date: Tue, 7 Mar 2017 23:09:20 -0500 Subject: [PATCH 3/6] curry randint_range --- toolz/curried/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/toolz/curried/__init__.py b/toolz/curried/__init__.py index 43aeffd4..5cc01fac 100644 --- a/toolz/curried/__init__.py +++ b/toolz/curried/__init__.py @@ -82,6 +82,7 @@ partitionby = toolz.curry(toolz.partitionby) pluck = toolz.curry(toolz.pluck) random_sample = toolz.curry(toolz.random_sample) +randint_range = toolz.curry(toolz.randint_range) reduce = toolz.curry(toolz.reduce) reduceby = toolz.curry(toolz.reduceby) remove = toolz.curry(toolz.remove) From 96eb762c64625744cc373672b7d532c5337e2d5a Mon Sep 17 00:00:00 2001 From: Richard Postelnik Date: Tue, 7 Mar 2017 23:16:16 -0500 Subject: [PATCH 4/6] fix pep8 styling --- toolz/itertoolz.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 23fdc30a..0a1e06a7 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -14,7 +14,8 @@ 'first', 'second', 'nth', 'last', 'get', 'concat', 'concatv', 'mapcat', 'cons', 'interpose', 'frequencies', 'reduceby', 'iterate', 'sliding_window', 'partition', 'partition_all', 'count', 'pluck', - 'join', 'tail', 'diff', 'topk', 'peek', 'random_sample', 'randint_range') + 'join', 'tail', 'diff', 'topk', 'peek', 'random_sample', + 'randint_range') def remove(predicate, seq): @@ -1003,4 +1004,3 @@ def randint_range(sample_size, min, max, random_state=None): yield i sample_size -= 1 population_size -= 1 - From 82b08bdec9e5ab2a8162bbf926701878fab8d06d Mon Sep 17 00:00:00 2001 From: Richard Postelnik Date: Tue, 7 Mar 2017 23:50:39 -0500 Subject: [PATCH 5/6] make it lazy for python2 --- toolz/itertoolz.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 0a1e06a7..dc131a83 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -999,6 +999,8 @@ def randint_range(sample_size, min, max, random_state=None): if not hasattr(random_state, 'random'): random_state = Random(random_state) population_size = max - min + if hasattr(__builtins__, 'xrange'): + range = xrange for i in range(min, max): if random_state.random() < sample_size / population_size: yield i From ee195ff8be645711d9e8bbb60d14d07c9726fa70 Mon Sep 17 00:00:00 2001 From: Richard Postelnik Date: Tue, 7 Mar 2017 23:55:35 -0500 Subject: [PATCH 6/6] Revert "make it lazy for python2" This reverts commit 82b08bdec9e5ab2a8162bbf926701878fab8d06d. --- toolz/itertoolz.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index dc131a83..0a1e06a7 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -999,8 +999,6 @@ def randint_range(sample_size, min, max, random_state=None): if not hasattr(random_state, 'random'): random_state = Random(random_state) population_size = max - min - if hasattr(__builtins__, 'xrange'): - range = xrange for i in range(min, max): if random_state.random() < sample_size / population_size: yield i