MCPcopy Create free account
hub / github.com/ipython/ipython / block_parser

Function block_parser

IPython/sphinxext/ipython_directive.py:226–327  ·  view source on GitHub ↗

part is a string of ipython text, comprised of at most one input, one output, comments, and blank lines. The block parser parses the text into a list of:: blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...] where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and data is, de

(part, rgxin, rgxout, fmtin, fmtout)

Source from the content-addressed store, hash-verified

224#-----------------------------------------------------------------------------
225
226def block_parser(part, rgxin, rgxout, fmtin, fmtout):
227 """
228 part is a string of ipython text, comprised of at most one
229 input, one output, comments, and blank lines. The block parser
230 parses the text into a list of::
231
232 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
233
234 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
235 data is, depending on the type of token::
236
237 COMMENT : the comment string
238
239 INPUT: the (DECORATOR, INPUT_LINE, REST) where
240 DECORATOR: the input decorator (or None)
241 INPUT_LINE: the input as string (possibly multi-line)
242 REST : any stdout generated by the input line (not OUTPUT)
243
244 OUTPUT: the output string, possibly multi-line
245
246 """
247 block = []
248 lines = part.split('\n')
249 N = len(lines)
250 i = 0
251 decorator = None
252 while 1:
253
254 if i==N:
255 # nothing left to parse -- the last line
256 break
257
258 line = lines[i]
259 i += 1
260 line_stripped = line.strip()
261 if line_stripped.startswith('#'):
262 block.append((COMMENT, line))
263 continue
264
265 if line_stripped.startswith('@'):
266 # Here is where we assume there is, at most, one decorator.
267 # Might need to rethink this.
268 decorator = line_stripped
269 continue
270
271 # does this look like an input line?
272 matchin = rgxin.match(line)
273 if matchin:
274 lineno, inputline = int(matchin.group(1)), matchin.group(2)
275
276 # the ....: continuation string
277 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
278 Nc = len(continuation)
279 # input lines can continue on for more than one line, if
280 # we have a '\' line continuation char or a function call
281 # echo line 'print'. The input line can only be
282 # terminated by the end of the block or an output line, so
283 # we parse out the rest of the input line if it is

Callers 1

runMethod · 0.85

Calls 1

groupMethod · 0.80

Tested by

no test coverage detected