In python2.7: 2 for loops are always a little inefficient, especially in python. Is there a better way to write the following filter function? It tags a line from a log file, if it is useful. Otherwise the line will be ignored. Because there are different possible interesting lines, it tries different compiled regexes for each line, until it finds one. Note that no more regexes are checked for a line, after the first one successfully matched.
def filter_lines(instream, filters): """ignore lines that aren't needed :param instream: an input stream like sys.stdin :param filters: a list of compiled regexes :yield: a tupel (line, regex) """ for line in instream: for regex in filters: if regex.match(line): yield (line,regex) break
(The "tagging" is done with the regex object itself, because it can be used later on for retrieving substrings of a line, like filename and row number in an occuring error)