SnakeViz 0.1 – A Python Profile Viewer

When profiling code it can be helpful to have a visualization of the profile to really show what areas of your program are taking the most time. The great RunSnakeRun application has been filling this roll for a while now but some of my colleagues have been turned off by the need to install wxPython. The web browsers we use every day are quickly becoming great visualization platforms so I thought it would be a nice project to make a Python profile viewer that works in the browser. Today, co-developer Erik and I are happy to announce the first official release of SnakeViz.

Example SnakeVis visualization.
An example SnakeViz visualization.

Continue reading “SnakeViz 0.1 – A Python Profile Viewer”

SnakeViz 0.1 – A Python Profile Viewer

What’s In My Stack?

This is a response to a post on the Software Carpentry blog: What’s In Your Stack?

My development setup is pretty simple:

  • Sublime Text 2 with a few plugins
  • git, GitHub, and bitbucket
  • Subversion and Trac (only at work)
  • IPython notebook
  • NumPy, SciPy, and matplotlib (mostly at work)
  • Chrome for debugging web stuff
  • bash and the usual shell utilities

It should go without saying that I’m using all of this to work on Python. And a little  JavaScript comes in for web apps.

What’s in your stack?

What’s In My Stack?

Function Timeout in Python

In some code I’m working on I want to put a 10 second timeout on a function call. If the function doesn’t return in 10 seconds I just want to terminate the function and return None. Here’s the code I came up with using multiprocessing.Pool:

import multiprocessing as mp

pool = mp.Pool(1, maxtasksperchild=1)
result = pool.apply_async(function, (arg1,))
pool.close()

try:
    s = result.get(10)
except mp.TimeoutError:
    pool.terminate()
    return None

This seems to work but I’m curious if there’s a standard Python pattern for this sort of thing?

Function Timeout in Python

Testing Python with the mock library

A bit ago I was writing some unit tests for a complicated code at work and when I looked at all of the crazy things I’d need to instantiate just to run this one little unit I despaired. Looking at the code it seemed like the usage of the input objects was pretty simple though, maybe, I thought, I could just write fake little stub objects to stand in for them and use those…

But then it occurred to me, that’s what mock objects are for! Mock objects are useful in unit testing as stand-ins for other functions or objects. You might use mock objects when, for example, the real thing is expensive to create, requires online resources that might sometimes be offline, or when you just want to do really fine-grained testing. With mock objects you can easily control exactly what they do and then test whether your code-under-test used the mock object correctly.

I picked the mock library as it seemed fairly straightforward and the documentation is copious, though dense. I also found the examples to not be very helpful. There are other options. All-in-all I’d recommend mock because once I got the hang of the usage it was really simple to use.

I am apparently the first person in my group to ever try to use mock objects so today I gave a lesson both on the concept of mocks and the usage of the mock library. My notebook is available:

Testing Python with the mock library