You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As raised recently on python-ideas [1]_, an itertools method fixing
iterators to a certain length might be handy (where fixing is either
cutting elements off or appending values).
I appended a patch implementing this feature in Python/C, unit tests and
documentation included.
"Return an iterator fixing *iterable*\ 's length to *length* by either
cutting elements off or adding *value* to the iterable."
Rather than mixing both, it would be more flexible to have a separate
function for each IMO.
Cutting off is already be handled by islice() (isn't it?), and padding
should be done with a function called e.g. pad() or rpad().
Am taking this under advisement but am initially disinclined. It is not
a basic itertool since it can be composed from the others. Also, it's
use cases seem to be few. One challenge for the module is that adding
more tools makes the overall toolkit harder to use because the number of
choices is increased.
lehmannro commented on Jan 23, 2009
lehmannromannequin
MannequinAuthor
More actions
When I started writing this patch this was actually what I intended. But
having fixlen(range(3), 2) return 0 1 2 struck me as odd. Renaming
the function to pad would help there indeed.
It depends on which use case is more common: either fixing an iterator
to a certain length (slicing/padding applied as required) or obtaining
an iterator of *at least* some number of elements (padded as required)?
The thread on python-ideas suggests the latter while the example Python
code brought up there implements the former. OTOH the latter cannot be
composed *that easily* of other itertools but the former is more useful
for unpacking.
On a related note: what should happen if length is negative? itertools.repeat just defaults to 0 in such cases but I am unsure how
applicable that is in this case.
Activity
lehmannro commentedon Jan 22, 2009
As raised recently on python-ideas [1]_, an itertools method fixing
iterators to a certain length might be handy (where fixing is either
cutting elements off or appending values).
I appended a patch implementing this feature in Python/C, unit tests and
documentation included.
.. [1] http://thread.gmane.org/gmane.comp.python.ideas/2472/focus=2479
pitrou commentedon Jan 22, 2009
"Return an iterator fixing *iterable*\ 's length to *length* by either
cutting elements off or adding *value* to the iterable."
Rather than mixing both, it would be more flexible to have a separate
function for each IMO.
Cutting off is already be handled by islice() (isn't it?), and padding
should be done with a function called e.g. pad() or rpad().
rhettinger commentedon Jan 22, 2009
Am taking this under advisement but am initially disinclined. It is not
a basic itertool since it can be composed from the others. Also, it's
use cases seem to be few. One challenge for the module is that adding
more tools makes the overall toolkit harder to use because the number of
choices is increased.
lehmannro commentedon Jan 23, 2009
When I started writing this patch this was actually what I intended. But
having
fixlen(range(3), 2)return 0 1 2 struck me as odd. Renamingthe function to
padwould help there indeed.It depends on which use case is more common: either fixing an iterator
to a certain length (slicing/padding applied as required) or obtaining
an iterator of *at least* some number of elements (padded as required)?
The thread on python-ideas suggests the latter while the example Python
code brought up there implements the former. OTOH the latter cannot be
composed *that easily* of other itertools but the former is more useful
for unpacking.
On a related note: what should happen if
lengthis negative?itertools.repeatjust defaults to 0 in such cases but I am unsure howapplicable that is in this case.
rhettinger commentedon Jan 24, 2009
Am still leaning towards rejecting this one based on: