The module code_utils.py
contains functions useful to deal with code.
Currently it only contains find_in_code()
function, described below
Finds all occurrences of the string string
in the code object code
,
optionally restricting the search to the lines specified in the lines
parameter. In the lines parameter, the lines start numbered at 1
to match the line numbers shown in the code object.
The result is a list of tuples (line_number, start, end)
where line_number
is the line number (starting at 0), and
start
and end
are the limits of a slice which contains the
glyphs that match the string. Note that in the result the line
numbers start at 0, not at 1 as in the lines
parameter, to make
easy to use the result with the code
object.
The result can be used to select the matching glyphs and apply a style or animation to them.
Example:
result = find_in_code(code, "foo", lines=[1, 3])
for line_number, start, end in result:
code.code[line_number][start:end].set_color(RED)
This scene shows the code of the scene himself, and highlights
the appearances of the string "code"
at lines 6 and 8.
The source code of this example is in the file example.pyp
,
and the resulting image is this: