import os12def file_to_worksheet(filename):3"""4Convert a Python file to a worksheet suitable for editing and debugging.5"""6r = open(filename).readlines()7for i in range(len(r)):8s = r[i].lstrip()9if s.startswith('sage:'):10r[i] = '{{{%s}}}\n'%(s[5:].rstrip())1112s = '<pre>' + ''.join(r) + '</pre>'13open(os.path.abspath(filename) + '.ws', 'w').write(s)1415## try:16## while True:17## i = i + find_first_prompt(s[i:])18## j = i + find_end_of_input(s[i:])19## k = j + find_end_of_block(s[j:])20## except ValueError:21## pass2223## def find_first_prompt(s):24## j = s.find('sage:')25## if j == -1:26## raise ValueError27## return j2829## def find_end_of_block(s):30## """31## Next blank line, triple quotes, or sage:32## """333435363738