Skip to content

Instantly share code, notes, and snippets.

@daniyalzade
Created November 18, 2012 23:12
Show Gist options
  • Save daniyalzade/4108018 to your computer and use it in GitHub Desktop.
Save daniyalzade/4108018 to your computer and use it in GitHub Desktop.
Log progress while iterating over a list
import logging
def _display_msg(msg, use_print=False):
"""
@param msg: str
@param use_print: bool
"""
if use_print:
print msg
return
logging.info(msg)
def adv_enumerate(list_,
start=0,
end=None,
frequency=100,
use_print=False,
get_tuple=False,
):
"""
@param list_: list
@param frequency: int
@param use_print: bool
@return: Generator
"""
len_ = 'NA'
try:
len_ = len(list_)
except TypeError:
pass
for i, item in enumerate(list_, start=start):
if not i % frequency:
_display_msg("completed %s/%s" % (i, len_))
if end and i >= end:
raise StopIteration
yield (i, item) if get_tuple else item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment