Created
March 28, 2011 16:20
-
-
Save sbp/890758 to your computer and use it in GitHub Desktop.
Run Python documentation as script content
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
| from itertools import groupby | |
| from operator import itemgetter | |
| def first(length): return itemgetter(slice(None, length)) | |
| def remove_indent(group, n): return (line[n:] for line in group) | |
| def combine(lines, sep): return sep.join(lines) + sep | |
| def indented_sections(text): | |
| """ | |
| >>> example = 'One:\n abc\n def\nTwo:\n ghi' | |
| >>> for section in indented_sections(example): | |
| ... print section.splitlines() | |
| ['abc', 'def'] | |
| ['ghi'] | |
| """ | |
| groups = groupby(text.splitlines(), first(3)) | |
| for prefix, group in groups: | |
| if prefix == ' ': | |
| yield combine(remove_indent(group, 3), '\n') | |
| for section in indented_sections(__doc__): | |
| if not section.startswith('>>>'): | |
| exec(section) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment