From 41ffc05d05bfa3849c8160dd7cfa95b1eb51bb61 Mon Sep 17 00:00:00 2001 From: Anthony LaTorre Date: Fri, 26 Aug 2011 18:57:02 -0400 Subject: cleanup repeating_iterator() docstring. --- itertoolset.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'itertoolset.py') diff --git a/itertoolset.py b/itertoolset.py index eb96e74..2d71d0c 100644 --- a/itertoolset.py +++ b/itertoolset.py @@ -3,20 +3,21 @@ import collections from copy import deepcopy def repeating_iterator(i, nreps): - '''Returns an iterator that emits each element of `i` multiple times specified by `nreps`. - The length of this iterator is the lenght of `i` times `nreps`. This iterator - is safe even if the item consumer modifies the items. + """Returns an iterator that emits each element of `i` multiple times + specified by `nreps`. The length of this iterator is the lenght of `i` + times `nreps`. This iterator is safe even if the item consumer modifies + the items. - >>> list(repeating_iterator('ABCD', 3) - ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D'] - >>> list(repeating_iterator('ABCD', 1) - ['A', 'B', 'C', 'D'] - ''' + Examples: + >>> list(repeating_iterator('ABCD', 3) + ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D'] + >>> list(repeating_iterator('ABCD', 1) + ['A', 'B', 'C', 'D'] + """ for item in i: for counter in xrange(nreps): yield deepcopy(item) - def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n -- cgit From dd4085d7ca49bbde2826192716c1aa7c69b4bcc6 Mon Sep 17 00:00:00 2001 From: Anthony LaTorre Date: Fri, 26 Aug 2011 19:10:10 -0400 Subject: add repeat_func() to itertoolset. --- itertoolset.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'itertoolset.py') diff --git a/itertoolset.py b/itertoolset.py index 2d71d0c..8d94e14 100644 --- a/itertoolset.py +++ b/itertoolset.py @@ -2,6 +2,15 @@ from itertools import * import collections from copy import deepcopy +def repeat_func(func, times=None, args=()): + "equivalent to (func(*args) for i in xrange(times))." + if times is None: + while True: + yield func(*args) + else: + for i in xrange(times): + yield func(*args) + def repeating_iterator(i, nreps): """Returns an iterator that emits each element of `i` multiple times specified by `nreps`. The length of this iterator is the lenght of `i` -- cgit From 707ca1b366f11032682cc864ca2848905e6b485c Mon Sep 17 00:00:00 2001 From: Anthony LaTorre Date: Fri, 2 Sep 2011 12:12:38 -0400 Subject: update event structure. break gpu.GPU class into separate smaller independent classes. --- itertoolset.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'itertoolset.py') diff --git a/itertoolset.py b/itertoolset.py index 8d94e14..498b940 100644 --- a/itertoolset.py +++ b/itertoolset.py @@ -2,6 +2,19 @@ from itertools import * import collections from copy import deepcopy +def peek(iterable): + """Peek at the first element of `iterable`. + + Returns a tuple of the form (first_element, iterable). + + Once peek() has been called, the original iterable will be modified if it + was an iterator (it will be advanced by 1); use the returned iterator for + an equivalent copy of the original iterable. + """ + it = iter(iterable) + first_element = next(it) + return first_element, chain([first_element], it) + def repeat_func(func, times=None, args=()): "equivalent to (func(*args) for i in xrange(times))." if times is None: @@ -11,6 +24,16 @@ def repeat_func(func, times=None, args=()): for i in xrange(times): yield func(*args) +def repeat_copy(object, times=None): + """Returns deep copies of `object` over and over again. Runs indefinitely + unless the `times` argument is specified.""" + if times is None: + while True: + yield deepcopy(object) + else: + for i in xrange(times): + yield object + def repeating_iterator(i, nreps): """Returns an iterator that emits each element of `i` multiple times specified by `nreps`. The length of this iterator is the lenght of `i` -- cgit