Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/misc/file_to_worksheet.py
4058 views
1
import os
2
3
def file_to_worksheet(filename):
4
"""
5
Convert a Python file to a worksheet suitable for editing and debugging.
6
"""
7
r = open(filename).readlines()
8
for i in range(len(r)):
9
s = r[i].lstrip()
10
if s.startswith('sage:'):
11
r[i] = '{{{%s}}}\n'%(s[5:].rstrip())
12
13
s = '<pre>' + ''.join(r) + '</pre>'
14
open(os.path.abspath(filename) + '.ws', 'w').write(s)
15
16
## try:
17
## while True:
18
## i = i + find_first_prompt(s[i:])
19
## j = i + find_end_of_input(s[i:])
20
## k = j + find_end_of_block(s[j:])
21
## except ValueError:
22
## pass
23
24
## def find_first_prompt(s):
25
## j = s.find('sage:')
26
## if j == -1:
27
## raise ValueError
28
## return j
29
30
## def find_end_of_block(s):
31
## """
32
## Next blank line, triple quotes, or sage:
33
## """
34
35
36
37
38