Created
November 18, 2012 23:12
-
-
Save daniyalzade/4108018 to your computer and use it in GitHub Desktop.
Log progress while iterating over a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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