Imagine that you could open a window that showed the contents of a variable and updated itself as the variable changed!
I’ve been writing - and debugging - some edit-distance-type code in Python. Its lots of big numpy arrays.
The super-cool APL game-of-life video shows how APL can open a watch window on a variable. Its so cool; I want to be able to inspect my numpy arrays live as my algorithm executes. Simply printing them to console means things scroll off-screen pretty quickly and its distracting.
I asked around but couldn’t find any watch window for Python.
So, to avoid solving my bigger problems, I quickly wrote one and flushed that procrastination right out of me!
import debugwin and then
debugwin.watch(your_var). This will create a special window showing the contents of the variable and automatically updating itself as the variable contents change.
If you want you can track the id returned by the watch function and use it to
unwatch your variable.
And it works great. I am going through my programs removing debug logging and just invoking a watch window instead. The problem with classic console logging is that it scrolls up the screen; with this window approach, you can much more easily inspect large data-structures live. I could actually spy on my DNA matching heuristic algorithms just by watching the array of sequence strings.
And at work I’ve just added one line that lets me watch a task queue in a server while it runs its integration tests. It doesn’t get easier than that!
debugwin runs Tkinter in its own thread (this may conflict with any Tkinter UI you already have; in which case I’m afraid you’re best surviving without debugwin) and polls all the watch variables 10x per second or so; it has minimal impact on your program.
It is incredibly basic. It just uses
str() to get a representation of an object. The only finesse is that it tries to acquire a weak-reference to your variable which will automatically close the watch window if your variable gets garbage-collected.
You can create your own pretty-printer by creating a shim object that formats in its
I hope its useful to you! I’d be more than happy if you adopted it, enhanced it, published it etc. Things I’d be happy if you added:
- pretty printers for common structures like dictionaries and generic object instances; ideally with roll-up
- a console class; with book-marking, so you can delete or replace sections of the output
- decent default watch window positions so they don’t stack on top of each other etc
↓ click the "share" button below!