EuroPython 2018: Debugging Your Code with Data Visualization

James Saryerwinnie is a Software Development Engineer at Amazon Web Services.


Studies claim that we spend 30-50% of our time debugging, but however much it is, and it's probably always more than we expect. Debuggers are, according to another studies, mostly good programmers, but the other way is not as true. Debugging is a separate skill, and very important.

There's print/log debugging, interactive debugging, and core debugging. Today we'll introduce a new method: visualisation debugging. This helps to build a mental model of how the code works and how it's supposed to work. The visualisations are just meant for understanding – throw away any changes just as you remove print statements.

Sequence diagram

Use seqdiag (on pypi) to build sequence diagrams by hand. But instead of building it by hand, you can just add a print statement to the relevant methods to your code printing sequence diagram lines they're performing. You can then generate a diagram, which may make things clearer.

Visualising internal state

Consider visualising the internal state of an internal object or file. For files you can for example use matplotlib to visualise write locations. Log write locations (in the case of write objects), then generate charts. Use this especially for threaded programming, because you can use timestamped logging of putting one thread into context with other threads.

Generate custom diagrams

Sometimes the existing tools don't cut it in visualising your mental model. E.g. make sure you are sure if you want to focus on state or event logging. Consider using Pillow (a fork of the python imaging library), which has an ImageDraw module for generating images from scratch.