StopIteration exception in tools.batch() under Python 3.7.1
Hello,
Running python -m flatisfy import --config config.json
under Python 3.7.1 gives me the following error:
Traceback (most recent call last):
File "/home/guatto/src/Flatisfy/flatisfy/tools.py", line 175, in batch
yield itertools.chain([next(batchiter)], batchiter)
StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/guatto/src/Flatisfy/flatisfy/__main__.py", line 228, in <module>
main()
File "/home/guatto/src/Flatisfy/flatisfy/__main__.py", line 214, in main
cmds.import_and_filter(config, load_from_db=False)
File "/home/guatto/src/Flatisfy/flatisfy/cmds.py", line 141, in import_and_filter
fetched_flats = fetch.fetch_flats(config)
File "/home/guatto/src/Flatisfy/flatisfy/fetch.py", line 271, in fetch_flats
queries = webOOB_proxy.build_queries(constraint)
File "/home/guatto/src/Flatisfy/flatisfy/fetch.py", line 142, in build_queries
for cities_batch in tools.batch(matching_cities, 3):
RuntimeError: generator raised StopIteration
It seems that wrapping the loop in tools.batch within a try block fixes the problem. But I'm no Python programmer and can't tell whether that's the right solution.
def batch(iterable, size):
sourceiter = iter(iterable)
try:
while True:
batchiter = itertools.islice(sourceiter, size)
yield itertools.chain([next(batchiter)], batchiter)
except StopIteration:
pass