-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetexamples.py
45 lines (34 loc) · 1003 Bytes
/
getexamples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from pathlib import Path
from textwrap import dedent
INDENT = ' '
def getexamples():
with (Path(__file__).parent / 'README.md').open() as f:
return collectexamples(f)
def collectexamples(lines):
examples = []
current = []
inexamples = False
for line in lines:
if line.strip() == '## Examples':
inexamples = True
continue
if line.startswith('## '):
inexamples = False
continue
if not inexamples:
continue
if line.startswith(INDENT):
current.append(line)
elif line.strip() and current:
examples.append(dedent(''.join(current)))
current = []
if current:
examples.append(''.join(current))
return examples
if __name__ == '__main__':
import sys
args = sys.argv[1:]
x = int(args[0]) if args else -1
for i, example in enumerate(getexamples()):
if x == -1 or i == x:
print(example)