CoCalc Public Fileswww / talks / pyrex / misc / extract.pyOpen with one click!
Author: William A. Stein
1
#!/usr/bin/python
2
3
# Extracts all the python code from entec.tex and puts it in the file entec.py
4
5
import os, sys
6
7
F = open("bsd.tex","r")
8
P = open("examples.txt","w")
9
P.write("##################################################\n")
10
P.write("# ent.py -- Element Number Theory \n")
11
P.write("# (c) William Stein, 2004\n")
12
P.write("##################################################\n\n\n\n")
13
14
max_len = 69
15
16
output = False
17
section = ""
18
session = False
19
sessions = ""
20
for x in F.readlines():
21
if x.find("section") != -1:
22
i = x.find("{")
23
j = x.find("}")
24
section = x[i+1:j]
25
if x.find("begin{verbatim}") != -1:
26
output = True
27
if section != "" and session == False and section!="intersection.":
28
P.write("\n##################################################\n")
29
P.write("## %s\n"%section)
30
P.write("##################################################\n\n")
31
section = ""
32
continue
33
elif x.find("end{verbatim}") != -1:
34
output = False
35
session = False
36
P.write("\n")
37
continue
38
elif x.find("%session") != -1:
39
session = True
40
if output:
41
if len(x) >= max_len:
42
x += "# ****** overfill (len = %s) ******\n"%len(x)
43
if session:
44
sessions += x
45
else:
46
P.write(x)
47
48
P.write("\n\n##########################################################\n")
49
P.write("# The following are all the examples not in functions. #\n")
50
P.write("##########################################################\n\n")
51
P.write('def examples():\n """\n')
52
for x in sessions.split("\n"):
53
P.write(" %s\n"%x)
54
P.write(' """\n\n')
55
56
P.write("""
57
if __name__ == '__main__':
58
import doctest, sys
59
doctest.testmod(sys.modules[__name__])
60
""")
61