RegEx to Convert String in TitleCase (Python / Django)

During web development I very often come across a problem to convert any string into TitleCase. Those who don’t know title case is the style of writing having first letter as a majuscule (upper-case letter) and the remaining letters in minuscules (lower-case letters).

So here is the python function to do that:

def title(value):
    """Converts a string into titlecase."""
    t = re.sub("([a-z])'([A-Z])", lambda m:, value.title())
    return re.sub("\d([A-Z])", lambda m:, t)

Happy Coding

Read More …

Debugging Django on Windows : Developement Setup

Django I remember the days when I used to struggle while debugging django web applications. But after working for more than a year now with django, I am more or less settled with tools I use for debugging Django Application. My Development System is on Windows XP, so this post is specifically for Debugging django on windows.

Debugging Django on Windows XP

The best tool I found on Windows environment for Django/ Python Debugging is : WinPDB, Just download and install.

Read More …

Scaling Your Frontend: Far-Futures Expire Headers

To optimize our website load times we must optimize the frontend by setting far-futures headers. This simply means that media has an Expires tag of sometime in the distant future (maybe a year), and when we make changes, the filename needs to change. The easiest way to do this, is by adding a simple GET parameter.

We can do this by using a simple template tag (or a global object as it’s known in Jinja), to output the URL to the file, as well as the revision.

Read More …

Django ORM : What to do when the memory requirement goes high?

As the title suggest this post is regarding “Memory Requirement of django ORM”.
Actually some days back I myself faced this problem and wrote “Why Django ORM Sucks : It takes a hell lot of memory in processing“. From various comments and a thread which started on google groups, I came up with few points one should check while operating on large amount of data.

For showing points i’ll use following example:

for r in UserRating.objects.all():
	ratingDict = cache.get(
	if ratingDict is None:
		cache.set(, [(, r.rating)], 86400)
		ratingDict.append((, r.rating))
		cache.set(, ratingDict, 86400)

So here are the mistakes in previous code.

Read More …

Django Example: FileField and ImageField

The syntax to add it in Model is:

class MyModel(models.Model):
	video = FileField(upload_to=None[, max_length=100, **options])

Here upload_to is local filesystem path that will be appended to your MEDIA_ROOT setting to determine the value of the url( attribute.

Setting MEDIA_ROOT and upload_to

MEDIA_ROOT = ‘/home/myname/files/
file: abc.flv
results in: /home/myname/files/videos/abc.flv

where as if,
MEDIA_ROOT = ‘/home/myname/files/’
and file : abc.flv
result will be saved in : ‘/videos/abc.flv’ (i.e., root directory of filesystem)

You can also set upload_to=’videos/%Y/%m/%d’.

Read More …

Why Django ORM Sucks : It takes a hell lot of memory in processing.

For a project i wanted to keep one of my table in memcache. I tried to do it using Django ORM but it was taking a hell lot of memory.

Let me tell u some details.

Table Structure : This table has almost 1,300,000 rows

id movie_id user_id rating
1 1 1 5.0
2 1 2 4.5
3 1 3 4.5
4 2 1 3.5
5 3 3 2.0

All I wanted was to keep them in cache(memcached), with following format:
memcache Key : movie-id
memcache Data : [ (user1:rating1), (user2:rating2) ]

First Approach : Directly from DB

for r in FlixsterUserRating.objects.all(): 
	ratingDict = cache.get( 
	if ratingDict is None:  
		cache.set(, [(, r.rating)], 86400) 
		ratingDict.append((, r.rating))  
		cache.set(, ratingDict, 86400) 
	del ratingDict

In this approach for processing every 1000 it was taking some 5.1 secs.

Read More …

Installing Memcached for Django Application on Windows XP

For a project I needed Memcached. But the problem was that I have my development system on WindowsXP.

When I Google I got various links to install Memcached on windows, but there wasn’t any link that tell all about Memcached with Django on Windows

So Here we go:

  • Download memcached 1.2.4 Win32.
  • Unzip the binaries in your desired directory (eg. c:\memcached)
  • Install the service using the command: ‘c:\memcached\memcached.exe -d install’ from either the command line
  • Start the server from the Microsoft Management Console or by running the following command: ‘c:\memcached\memcached.exe -d start’
  • Use the server, by default listening to port 11211
  • Now we have to get something so that python can talk with our memcached server.
  • Read More …