26 octubre
2015

Django, SQLite, GLOB, CAST and sorting

... or how to sort (properly) by a char field containing number values

Some days ago I found an interesting dilemma while working on a django based application. The application has a model similar to this one:

class Customer(models.Model):
    code = models.CharField(_('Customer code'), max_length=10)
    joined = models.DateField(_('Joined date'), auto_now_add=True)
    first_name = models.CharField(_('First name'), max_length=255)
    last_name = models.CharField(_('First name'), max_length=255)
    email = models.EmailField(_('Email address'))

    ...

That code attribute is a CharField where alphanumeric customer codes are stored. Those codes are mostly numbers (1,2,3...N) but then there are also codes like 2a, 3-groupB, etc.

At one point, it became useful to sort such users based on that code. If that would be an IntegerField, it would be as easy as using order_by:

from .models import Customer
customers = Customer.objects.all().order_by('code')

Or, if you want the results sorted in descending order:

customers = Customer.objects.all().order_by('-code')

But then, this was not an IntegerField, but a CharField, which means that using order_by would return something like:

0
1
10
11
12
13
...
2
20
21
...
3
...

And so on. Not exactly what was needed.

The following is the write-up covering the little adventure that was the work to fix it.

Continue reading "Django, SQLite, GLOB, CAST and sorting"
Posted by wu at 10:07 | Comments (2) | Trackbacks (0)
29 octubre
2015

File locking in python

... or how to prevent (periodic) processes overlap

So, let's keep up with the practical techie posts.

If you do software development, almost any kind of it, at one point you will find this scenario where you are running some code in a process in a cron job (or any kind of periodic scheduler). Now, the crontab entry sets that this process has to be run, for example, each 5 minutes. One day 5 minutes is not enough for that process (that usually takes less than a minute) to finish... and there it goes the next call to run that code.

"What could go wrong?" (TM)

Well, depending on the code, maybe nothing happens, maybe an ugly mess will turn a nice day into a nightmare or maybe you will get a call in the middle of the night urging you to fix it ASAP.

Continue reading "File locking in python"
Posted by wu at 09:35 | Comments (1) | Trackbacks (0)