4
prompt_start = r'>>>|<<<|\?\?\?|>>\?'
5
prompt_other_lines_start = r'==>|\.\.\.'
6
pattern = (r'^(?:%s).*?\n' % prompt_start) + r'(?:(?:%s).*?\n)*' % ( prompt_other_lines_start) + \
8
#~ pattern = r'^(?:%s).*?\n' % prompt_start
10
print "pattern:", repr(pattern)
11
re_code_block = re.compile(r'(%s)' % pattern, re.DOTALL+re.MULTILINE)
12
re_prompt_start = re.compile(prompt_start)
14
#~ empty_space_atend = re.compile(r'\s+$', re.MULTILINE)
15
def structure_example(intro, code_block):
16
#~ if intro.strip('\n\t ') = '':
18
full_txt = intro + code_block
21
while code_block[:-extra_empty_lines-1].endswith('\n'):
22
extra_empty_lines += 1
26
code_block = code_block[:-extra_empty_lines]
30
interaction_output = ''
32
if '==>' in code_block:
33
pos = code_block.find('==>')
34
prompt, expected_input = code_block[:pos], code_block[pos:]
36
#~ if not expected_input:
37
code_lines = prompt.split('\n')
38
for lineno, line in enumerate(code_lines):
39
#~ if not re_prompt_start.match(line):
40
if not line[:3] in ['>>>', '<<<', '???', '...', '>>?']:
41
prompt = '\n'.join(code_lines[:lineno])
42
interaction_output = '\n'.join(code_lines[lineno:])
44
#~ if expected_input and not expected_input.startswith('==>'):
45
#~ expected_input = '==>' + expected_input
51
expected_input=expected_input,
52
interaction_output=interaction_output,
53
extra_empty_lines=extra_empty_lines,
57
def txt2structured_examples(txt):
58
examples = re_code_block.split(txt+'\n')
59
#~ exampls_struct = [structure_example(intro, code_block)
60
structured_examples = []
62
for i, x in enumerate(examples):
66
structured_examples.append( structure_example(intro, code_block=x) )
68
return structured_examples
70
if __name__ == '__main__':
71
import analyse_tutorial_text
73
#~ txt = analyse_tutorial_text.init_text('../tutorials/py/intro.txt')
76
== Elementary Operations ==
78
First of all, computers must compute (count). try:
86
Also they can write :)
87
>>> print 'Hi' # or could also use double quotes: "
90
from pprint import pprint
92
examples = re_code_block.split(txt)
93
structured_examples = txt2structured_examples(txt)
95
print 'structured_examples'
96
pprint (structured_examples)
97
print 'len(structured_examples):', len(structured_examples)
98
print 'len(examples):', len(examples)