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(“”, “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.

Having issues with your first Django deployment on Heroku?

After spending a part of the night swearing at Heroku for not seeing my Django app correctly, I have realized it requires a very specific folder structure to operate properly. In my case, my whole Django project was in a subdirectory of my git repo (git_root > django_project > django_project >, while Heroku expects […]

How to fix long page load times on Heroku

If you have a free Heroku instance, you might sometimes face a long first page load time on your web application. This is because Heroku will shut down your instance after a while if nobody visits your site. This is a problem I have encountered with Bill Splitter since my girlfriend and I were the […]

Tastypie: only allow a user to use its own resources

If you want to limit user access to the resources they own in Tastypie, define obj_create and apply_authorization_limits as such. This will automatically assign created items to the current user, and only return resources that belong to the current user. from tastypie.resources import ModelResource from notes.models import Note class NoteResource(ModelResource): class Meta: queryset = Note.objects.all() […]

How to redo a South migration after using –fake

If you are using Django South, you might accidentally use migrate [app] –fake at some point. To undo your mistake, use the following commands: python shell from south.models import MigrationHistory m = MigrationHistory.objects.get(migration=”<migration_name>”) m.delete() That’s it! You can now safely perform your migration again.

How to clone model instances in Django

Let’s say you want to create an exact copy of your an object instance. All you need to do is set the primary key to None and save the object again. object_copy = MyObject.objects.get(pk=…) = None Tada! You now have a copy of your object with a new primary key.

Django: How to remove fields from a Form

Let’s say you want to inherit from the following form: class SuperLongForm(forms.Form): first_name = … last_name = … … And that you want to recreate the same form, but without the last_name field. All you have to do is to remove the field from the fields dict: class FirstNameOnlyForm(SuperLongForm): def __init__(self, *args, **kwargs): super(FirstNameOnlyForm, self).__init__(*args, […]

Django unit tests: How to populate request.raw_post_data

If you are creating unit tests with Django, you might need to send raw data with the Client object. In the example below, we are testing how our view reacts to a raw POST string sent by another API. The string in question is myrawpoststring. All you need to do is set the data attribute […]