MCPcopy
hub / github.com/satwikkansal/wtfpython / parse_example_parts

Function parse_example_parts

irrelevant/notebook_generator.py:87–183  ·  view source on GitHub ↗
(lines, title, current_line)

Source from the content-addressed store, hash-verified

85
86
87def parse_example_parts(lines, title, current_line):
88 parts = {
89 "build_up": [],
90 "explanation": []
91 }
92 content = [title]
93 statements_so_far = []
94 output_so_far = []
95 next_line = current_line
96 # store build_up till an H4 (explanation) is encountered
97 while not (next_line.startswith("#### ")or next_line.startswith('---')):
98 # Watching out for the snippets
99 if next_line.startswith("```py"):
100 # It's a snippet, whatever found until now is text
101 is_interactive = False
102 output_encountered = False
103 if content:
104 parts["build_up"].append(generate_markdown_block(content))
105 content = []
106
107 next_line = next(lines)
108
109 while not next_line.startswith("```"):
110 if is_interactive_statement(next_line):
111 is_interactive = True
112 if (output_so_far):
113 parts["build_up"].append(generate_code_block(statements_so_far, output_so_far))
114 statements_so_far, output_so_far = [], []
115 statements_so_far.append(next_line)
116 else:
117 # can be either output or normal code
118 if is_interactive:
119 output_so_far.append(next_line)
120 elif output_encountered:
121 output_so_far.append(next_line)
122 else:
123 statements_so_far.append(next_line)
124 next_line = next(lines)
125
126 # Snippet is over
127 parts["build_up"].append(generate_code_block(statements_so_far, output_so_far))
128 statements_so_far, output_so_far = [], []
129 next_line = next(lines)
130 else:
131 # It's a text, go on.
132 content.append(next_line)
133 next_line = next(lines)
134
135 # Explanation encountered, save any content till now (if any)
136 if content:
137 parts["build_up"].append(generate_markdown_block(content))
138
139 # Reset stuff
140 content = []
141 statements_so_far, output_so_far = [], []
142
143 # store lines again until --- or another H3 is encountered
144 while not (next_line.startswith("---") or

Callers 1

Calls 3

generate_markdown_blockFunction · 0.85
is_interactive_statementFunction · 0.85
generate_code_blockFunction · 0.85

Tested by

no test coverage detected