Use voussoirkit.gentools for chunk_generator.

master
voussoir 2020-11-15 22:07:41 -08:00
parent 3f32ab1664
commit 5a868dc7bd
2 changed files with 2 additions and 19 deletions

View File

@ -1,21 +1,3 @@
def chunk_sequence(sequence, chunk_length, allow_incomplete=True):
'''
Given a sequence, yield lists of length `chunk_length`.
allow_incomplete:
If True, allow the final chunk to be shorter if the
given sequence is not an exact multiple of `chunk_length`.
If False, the incomplete chunk will be discarded.
'''
import itertools
iterator = iter(sequence)
while True:
chunk = list(itertools.islice(iterator, chunk_length))
if not chunk:
break
if len(chunk) == chunk_length or allow_incomplete:
yield chunk
def truthystring(s): def truthystring(s):
if isinstance(s, (bool, int)) or s is None: if isinstance(s, (bool, int)) or s is None:
return s return s

View File

@ -1,6 +1,7 @@
import apiclient.discovery import apiclient.discovery
import isodate import isodate
from voussoirkit import gentools
from voussoirkit import vlogging from voussoirkit import vlogging
from . import helpers from . import helpers
@ -126,7 +127,7 @@ class Youtube:
def get_videos(self, video_ids): def get_videos(self, video_ids):
snippets = [] snippets = []
chunks = helpers.chunk_sequence(video_ids, 50) chunks = gentools.chunk_generator(video_ids, 50)
for chunk in chunks: for chunk in chunks:
self.log.debug('Requesting batch of %d video ids.', len(chunk)) self.log.debug('Requesting batch of %d video ids.', len(chunk))
self.log.loud(chunk) self.log.loud(chunk)