Rewrite chunk_sequence to work with generators.
At the cost of always returning lists instead of the input's type, which was never relevant anyway.
This commit is contained in:
parent
be45de55f5
commit
b1e35f6604
1 changed files with 11 additions and 15 deletions
|
@ -1,24 +1,20 @@
|
||||||
def chunk_sequence(sequence, chunk_length, allow_incomplete=True):
|
def chunk_sequence(sequence, chunk_length, allow_incomplete=True):
|
||||||
'''
|
'''
|
||||||
Given a sequence, divide it into sequences of length `chunk_length`.
|
Given a sequence, yield lists of length `chunk_length`.
|
||||||
|
|
||||||
:param allow_incomplete: If True, allow the final chunk to be shorter if the
|
allow_incomplete:
|
||||||
|
If True, allow the final chunk to be shorter if the
|
||||||
given sequence is not an exact multiple of `chunk_length`.
|
given sequence is not an exact multiple of `chunk_length`.
|
||||||
If False, the incomplete chunk will be discarded.
|
If False, the incomplete chunk will be discarded.
|
||||||
'''
|
'''
|
||||||
(complete, leftover) = divmod(len(sequence), chunk_length)
|
import itertools
|
||||||
if not allow_incomplete:
|
iterator = iter(sequence)
|
||||||
leftover = 0
|
while True:
|
||||||
|
chunk = list(itertools.islice(iterator, chunk_length))
|
||||||
chunk_count = complete + min(leftover, 1)
|
if not chunk:
|
||||||
|
break
|
||||||
chunks = []
|
if len(chunk) == chunk_length or allow_incomplete:
|
||||||
for x in range(chunk_count):
|
yield chunk
|
||||||
left = chunk_length * x
|
|
||||||
right = left + chunk_length
|
|
||||||
chunks.append(sequence[left:right])
|
|
||||||
|
|
||||||
return chunks
|
|
||||||
|
|
||||||
def truthystring(s):
|
def truthystring(s):
|
||||||
if isinstance(s, (bool, int)) or s is None:
|
if isinstance(s, (bool, int)) or s is None:
|
||||||
|
|
Loading…
Reference in a new issue