Created
May 29, 2012 14:38
-
-
Save Lysander/2828790 to your computer and use it in GitHub Desktop.
Alternative solution to topic: http://www.python-forum.de/viewtopic.php?f=1&t=29353
This file contains hidden or 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
| #!/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