Open in nbviewer Chrome Extension

I got tired of pasting URLs into nbviewer so I made a Chrome extension that will try to load your current page via nbviewer.

For example, if you are at https://gist.github.com/3778422 you can click the “Open in nbviewer” extension button and it will load http://nbviewer.ipython.org/3778422/ in a new tab. It also works for URLs ending in .ipynb.

You can download the extension from the Chrome Web Store and see the code on GitHub.

I took the extension icon from the nbviewer favicon, so thanks to them for that! Thanks also for making something as awesome as nbviewer, it’s getting so I couldn’t live without it!

Open in nbviewer Chrome Extension

Teaching with the IPython Notebook

For a few months now I’ve been using the IPython Notebook as my primary teaching tool for Python topics. Within Software Carpentry we’re also switching over to using the Notebook for both in-person bootcamps and our online repository of material. Ethan White and I put together a post on this topic on the Software Carpentry blog and now Titus Brown has blogged with his own thoughts. We’ve put in a PyCon proposal for a panel on this topic in 2013.

The IPython developers have to be given a huge amount of credit for putting together the Notebook and the rest of IPython. The Notebook especially is quite a feat: a top notch research/engineering/teaching tool all in one. And they aren’t resting on their laurels, they have a ton of ideas in mind for the Notebook in the future, including a slide-show mode. I’m definitely looking forward to seeing what they’ve got!

As with many open source projects, the IPython developers struggle to find the time and funding to write their software. If any open source project is helping with your job or your research you can easily help by citing the software in your papers and in public on social media or blogs. This gives the developers more ammunition the next time they’re writing grants, so please make your support known!

Teaching with the IPython Notebook

Job Search

That’s right I’m looking for a new job! I’m being picky though. I’ve already got an awesome job in scientific Python that’s not in California, so I’m looking for a position that is in California. I’ve already got a couple of good leads at San Francisco tech startups (wish me luck!), but if anyone knows of other positions using Python, especially supporting scientific work with Python, I’d appreciate a pointer! Thanks!

A little about me:

See more in my resume and look me up on GitHub, Google+, Twitter, and Alpha. Email me at jiffyclub .at. gmail .dot. com.

Job Search

Decode

This week I was in a job interview and one of my interview questions turned into a pretty interesting puzzle. I was asked the following question:

Assume a character encoding a = 1, b = 2, c = 3, … , z = 26. Given a string of numbers (e.g. '123456'), figure out how many different ways that string could be decoded.

I didn’t arrive a complete solution in the few minutes I had during the interview but I kept thinking about it afterwards because I couldn’t shake the feeling there was really a math problem here, not just a programming problem.

The difficult part of this challenge is figuring out how many decodings there are when there are overlapping possible groupings of numbers. For example, the string '111' can be decoded as ['1', '1', '1']['11', '1'], or ['1', '11']. I never figured out a formula for calculating the number of decodings but by manually figuring out the number of decodings for the strings '1''11''111''1111''11111''111111', etc. I noticed that the number of decodings follow the Fibonacci sequence starting with 1, 2, 3, 5, 8…! Unfortunately I don’t have a good explanation for why that is the case. (Though it makes gut level sense in the way the Fibonacci sequence is a sum of everything that has come before.) Please leave a comment if you can explain this!

So, my strategy for solving this problem follows these basic steps:

  1. Break the given string down into substrings such that each substring is one digit, '10' or '20', or cannot be broken down further because it has an unknown number of possible decodings. These substrings can be considered independently for the purposes of this problem.
    • For example, '956' becomes ['9', '5', '6'] and '121956' becomes ['1219', '5', '6'].
  2. For each substring figure out the number of decodings. For single digits, '10', and '20' this is just 1. For other substrings this is the Nth value in the Fibonacci series 1, 2, 3, 5, 8… where N is the length of the substring.
  3. Multiply together all of the number of decodings for the individual substrings to get the number of of decodings for the original string of numbers.

You can see my code for this solution here: http://nbviewer.ipython.org/3831343/ (raw notebook).

Decode

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

IPython Notebook Viewer

The nice folks over at IPython have put together a nice viewer for IPython notebooks stored as GitHub gists: nbviewer.ipython.org/. Just enter a gist URL or number. It doesn’t actually store notebooks, just renders notebooks stored as gists.

I’ve had some notebooks as gists for a while and now I can make pretty links to them:

IPython Notebook Viewer