Category Archives: Python

Django snippet: reduce image size during upload

Just a quick python snippet I written today to reduce the image size during upload class PhotoField(forms.FileField, object): def __init__(self, *args, **kwargs): super(PhotoField, self).__init__(*args, **kwargs) self.help_text = “Images over 500kb will be resized to keep under 500kb limit, which may result in some loss of quality” def validate(self,image): if not str(image).split(‘.’)[-1].lower() in [“jpg”,”jpeg”,”png”,”gif”]: raise ValidationError(“File… Read More »

Creating custom log handler that logs to database models in django

I was told to create some kind of logging mechanism to insert logs into database, and make sure it’s generic. So after some thought I decided to mimic the builtin RotatingFileHandler and write a DBHandler. The RotatingFileHandler allows you to specify which file to write to and rotate files, therefore my DBHandler should also allow… Read More »

Python code snippet for DPS webservice

import suds url = “” client = suds.client.Client(url) #print client username = “USERNAME” password = “PASSWORD” trans = client.factory.create(‘TransactionDetails’) trans.amount = 1 trans.cardHolderName = “Test” trans.cardNumber = ‘4111111111111111’ trans.inputCurrency= ‘NZD’ trans.cvc2 = ‘000’ trans.dateExpiry = ‘0412’ trans.txnType = ‘Purchase’ print client.service.SubmitTransaction(username, password, trans)

Python Webcam Capture Code Snippet

Here is a small piece of code how to query your webcam and save the frame as an image. import sys import Image, ImageDraw, datetime import opencv #this is important for capturing/displaying images from opencv import highgui camera = highgui.cvCreateCameraCapture(0) def get_image(): for i in range(10): im = highgui.cvQueryFrame(camera) # Add the line below if… Read More »

Python decorator – caching your django functions

Prerequisites: – Python Memcahed Library – Memcached server CACHES = { ‘default’ : { ‘BACKEND’: ‘django.core.cache.backends.memcached.MemcachedCache’, ‘LOCATION’: [‘’], ‘KEY_PREFIX’: ‘YOUR_PREFIX’, } } Caching Code from django.core.cache import cache #get the cache key for storage def cache_get_key(*args, **kwargs): import hashlib serialise = [] for arg in args: serialise.append(str(arg)) for key,arg in kwargs.items(): serialise.append(str(key)) serialise.append(str(arg)) key… Read More »

Django common dictionary available to templates

We need to leverage TEMPLATE_CONTEXT_PROCESSORS, the following example uses the “home” application add this to TEMPLATE_CONTEXT_PROCESSORS = (“django.contrib.auth.context_processors.auth”, “django.core.context_processors.debug”, “django.core.context_processors.i18n”, “”, “django.core.context_processors.static”, “django.contrib.messages.context_processors.messages”, “home.context_processor.remote_ip”) in home application, create a python file called in the add a function like this: def remote_ip(request): return {‘remote_ip’: request.META[‘REMOTE_ADDR’]} for any templates needs to use this processor… Read More »

How to create a Linux daemon in Python

I was writing a Python program to perform some routine tasks on twitter API, and came across this very helpful article for creating daemon process in Python… thought you might be interested… A simple unix/linux daemon in Python