Skip to content

Instantly share code, notes, and snippets.

@Lysander
Created May 29, 2012 14:38
Show Gist options
  • Select an option

  • Save Lysander/2828790 to your computer and use it in GitHub Desktop.

Select an option

Save Lysander/2828790 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from itertools import groupby
TEXT = """
/path/to/bla/bla1
used in 3 files:
/path/to/other/file1
/path/to/other/file2
/path/to/other/file3
/path/to/bla/bla2
used in 2 files:
/path/to/other/file4
/path/to/other/file5
"""
def strip_needless_lines(lines, skip="used in"):
for line in lines:
if line.lstrip().startswith(skip):
continue
elif line.strip():
yield line
def mark_sections(lines, key_token="/"):
for line in lines:
if line.startswith(key_token):
key = line
else:
yield key, line.lstrip()
def parse(lines):
links = dict()
for key, group in groupby(mark_sections(strip_needless_lines(lines)),
key=lambda t: t[0]):
links[key] = [value for _, value in group]
return links
def main():
links = parse(iter(TEXT.split("\n")))
print(links)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment