-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexamples_to_markdown.py
74 lines (61 loc) · 2.64 KB
/
examples_to_markdown.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Puts hypercomplex/examples.py into markdown format for easy use in README.md"""
import os
import io
import sys
def get_print_results(lines):
header = "from hypercomplex import *\n"
code = header + '\n'.join(lines)
stdout = sys.stdout
sys.stdout = io.StringIO()
exec(code)
output = sys.stdout.getvalue().splitlines()
sys.stdout = stdout
results = []
i = 0
for line in lines: # Assumes each print remained on one line.
out = None
if line.strip().startswith('print'):
out = output[i]
i += 1
results.append((line, out))
return results
def example_to_markdown(example):
lines = example.splitlines()
start = f'{lines[0]}\n\n ```py\n'
lines = lines[1:]
longest = max(map(len, lines))
middle = []
for line, output in get_print_results(lines):
if output is not None:
padding = longest - len(line)
line += ' ' * padding + f' # -> {output}'
middle.append(f' {line}')
end = '\n ```\n'
return start + '\n'.join(middle) + end
def examples_to_markdown():
with open(os.path.join('hypercomplex', 'examples.py')) as f:
examples = [e.strip() for e in f.read().split('# %%')]
examples = [e for e in examples if e][1:]
markdown = '\n'.join(map(example_to_markdown, examples))
with open('examples.md', 'w') as f:
f.write(markdown)
print(f"Converted {len(examples)} examples to markdown.")
# print(markdown)
if __name__ == "__main__":
examples_to_markdown()
# This is the only multiline example and examples_to_markdown can't handle it. Saving it here to avoid manually making it again.
e_matrix_example = """
11. `e_matrix` of a number class gives the multiplication table of `e(i)*e(j)`. Set `string=False` to get a 2D list instead of a string. Set `raw=True` to get the raw hypercomplex numbers.
```py
print(O.e_matrix()) # -> e1 e2 e3 e4 e5 e6 e7
# -e0 e3 -e2 e5 -e4 -e7 e6
# -e3 -e0 e1 e6 e7 -e4 -e5
# e2 -e1 -e0 e7 -e6 e5 -e4
# -e5 -e6 -e7 -e0 e1 e2 e3
# e4 -e7 e6 -e1 -e0 -e3 e2
# e7 e4 -e5 -e2 e3 -e0 -e1
# -e6 e5 e4 -e3 -e2 e1 -e0
#
print(C.e_matrix(string=False, raw=True)) # -> [[(1 0), (0 1)], [(0 1), (-1 0)]]
```
"""