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
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”)
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.
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 > settings.py), while Heroku expects […]
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 […]
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() […]
If you are using Django South, you might accidentally use manage.py migrate [app] –fake at some point. To undo your mistake, use the following commands: python manage.py 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.
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=…) object_copy.pk = None object_copy.save() Tada! You now have a copy of your object with a new primary key.
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, […]
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 […]