Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Last active August 29, 2015 14:06
Show Gist options
  • Save Lucretiel/20b5e2672d03d8f77343 to your computer and use it in GitHub Desktop.
Save Lucretiel/20b5e2672d03d8f77343 to your computer and use it in GitHub Desktop.
'''
Say you have a logger that adds empty lines between each logged
event, and each event is one more more lines. You want to iterate
over each logged event.
'''
def sectionalize(lines):
'''
Divide lines into sections, separated by empty lines. Technically
you could come up with a regex and do re.split, but that's doesn't
sound easy or fun at all
'''
section = []
for line in lines:
line = line.rstrip()
if line: #If line is not empty, add to the section
section.append(line)
else: #If the line is empty, yield the section and reset it
if section:
yield section
section = []
def sectionalize2(lines)
'''
Exactly as above, but much more concise.
'''
return (list(section)
for has_data, section
in itertools.groupby(lines,
lambda line: bool(line.rstrip()))
if has_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment