Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to step through Python expression evaluation process?

I want to build a visual debugger, which helps programming students to see how expression evaluation takes place (how subexpressions get evaluated and "replaced" by their values, something like expression evaluation visualizer in Excel).

Looks like you can't step through this process with Python's pdb, as its finest step granularity is line of code. Is it somehow possible to step through Python bytecode? Any other ideas how to achieve this goal?

EDIT: I need a lightweight solution that can be built on top of CPython standard library.

like image 876
Aivar Avatar asked Dec 13 '12 11:12

Aivar


People also ask

How do you solve evaluating expressions?

To evaluate an algebraic expression, you have to substitute a number for each variable and perform the arithmetic operations. In the example above, the variable x is equal to 6 since 6 + 6 = 12. If we know the value of our variables, we can replace the variables with their values and then evaluate the expression.

Does a Python expression needs to be evaluated?

Python executes a statement by evaluating its expressions to values one by one, then performing some operation on those values. Python evaluates an expression by first evaluating its sub-expressions, then performing an operation on the values.

What is eval () function in Python?

The eval() function evaluates the specified expression, if the expression is a legal Python statement, it will be executed.


2 Answers

I have a solution idea also myself -- I could instrument the code (or AST) by wrapping all (sub)expressions in a dummy method call, which does nothing more than returning its argument. Eg.

x = f(sin(x + y))

becomes

x = dummy(f(dummy(sin(dummy(dummy(x) + dummy(y))))))

This way I'm guaranteed to be notified after each subexpression gets evaluated and I also get the values. I can also add extra location/AST information about which part of the expression is currently dealt with, eg:

... dummy(x, line=23, col=13, length=1) ...

Unfortunately this requires messing with AST and compilation ...

like image 154
Aivar Avatar answered Sep 28 '22 17:09

Aivar


Have you tried pudb? http://pypi.python.org/pypi/pudb On a debian-like: apt-get install python-pudb

It attaches to pdb, so I guess this is not what you're looking for. At least, when you step in a function, it clearly appears which one you're in.

For teaching students, something that you could can be:

  • first, write the program using variables, composing using several steps,
  • debug this program using whichever decent python debugger (pdb, winpdb, pudb ...),
  • then, once the process is well understood, get rid of temporary variables, by combining the code into fewer lines, gradually, until you come to the final code.

I know, it is far to be perfect, but this is the best I can think of, at the moment.

like image 41
Pierre Avatar answered Sep 28 '22 19:09

Pierre