How to fix ImportMismatchError in Python

If you get an ImportMismatchError when running a Python app, it’s likely that you have some Python bytecode files (*.pyc) from a different runtime. For example, this happens when I run my Python unit tests inside a Docker container, then try to run them again in PyCharm. Here is an example error message: /…/env/bin/python2.7 “/Applications/PyCharm […]

A heap implementation in Python

One more python implementation of a data structure. This is a heap implementation that follows an architecture similar to the binary search tree we have built earlier. from collections import Container class Heap(Container): array = [] # Allows “if item in tree:” def __contains__(self, item): return self._contains(item, 1) def _contains(self, item, index): if item > […]

A binary search tree in Python

As part of my series on concepts you should never, ever need to reimplement in a real world scenario, here is a binary search tree in Python. This particular example contains a bit of duplicate code, but I doubt the benefits of changing this would outweigh the loss in readability, especially for a learning example. […]

A bubble sort in Python

As a second exercise in preparation for my Microsoft phone interview, I give you the Python bubble sort (or sinking sort). Once again, this is just an exercise. You should use the standard Python facilities for your sorting needs, especially given the terrible efficiency of bubble sort. from random import shuffle countries = ["Afghanistan","Albania","Algeria", ...] […]

A binary search in Python

This week, I received some exciting news by email: I will be interviewed for an internship at Microsoft! In order to keep my memory fresh, I have decided to practice my algorithms and data structures, then post the results. To get things started, here is how you perform a binary search in Python, not that […]

Following redirects with the Django test client

When running unit tests with in Django, the test client’s default behaviour is to stop at the first response, even if that response is a redirect. If you want the client to follow these redirects and return the last page, perform your requests like this: response = c.get(‘/redirect_me/’, follow=True) This will also add response.redirect_chain so […]

Euclidean algorithm in Python

While it won’t really be useful to most people, here’s how you implement the Euclidean algorithm in Python. b = 13 a = 18 while b > 0: if a>b: a = a-b else: b = b-a print “The GCD is:” print a or b

How to download an image in Python

Sometimes, simple problems have simple solutions. Downloading images with Python is one of those simple problems. Nonetheless, here’s a simple snippet that shows you how to do it: import urllib urllib.urlretrieve(“http://www.nicolasbouliane.com/pictures/0001.jpg”, “my_picture_file.jpg”)

Install easy_install on Ubuntu and Linux Mint

If you need easy_install on your Ubuntu or Linux Mint machine, use the following command to install it: sudo apt-get install python-setuptools python-dev build-essential I strongly suggest that you use pip instead, but I’m leaving this here for those it might help.

Using angular.js with Tastypie for Django

The following angular.js service will allow you to consume a standard TastyPie REST API. As you may know, TastyPie returns its JSON objects a bit differently from what angular expects, so this service fixes it. This example assumes that you have always_return_data=True in your resource. This is the resource I used with this example: class […]