My Python & co. stuff

Archive for July 2009

… just use pg_ctl register:

C:> set PGDATA=c:\postgresql-8.4-data
C:> pg_ctl register -N PostgreSQL
C:> net start PostgreSQL

Not exactly Python, but should help for those of you out there, who are also using Vista… focus stealing prevention fix for Windows.

UPDATE: There is a Google Code project on django+postgresql+INTERVAL available here.

Here you are. Idea taken from zboczuch:

from datetime import timedelta
from django.conf import settings

class IntervalField(models.Field):

    def db_type(self):
        if not settings.DATABASE_ENGINE.startswith('postgresql'):
            raise NotImplementedError
        return "interval"

    def to_python(self, value):
        assert(isinstance(value, timedelta))
        return value

    def _seconds(self, value):
        return value.days * 24 * 60 * 60 + value.seconds + (value.microseconds / 1000000.0)

    def get_db_prep_value(self, value):
        if value is None: return None
        return '%f' % self._seconds(value)

Ugly and lame… but it is working! Just edit pysrc/ (on my machine it is somewhere, like, here: C:\eclipse-galileo\plugins\org.python.pydev.debug_1.4.6.2788\pysrc\ to use Twisted Trial with Eclipse’s PyDev “Run as Unit-test” option.

If you run a directory as a unit-test, the code below assumes, that tests are located in [directory]/test . If you run a file as a unit test, the code assumes, that the test file is located in ./test/test_[filename].py .

#======================================================================================================================= # main #======================================================================================================================= if __name__ == '__main__': dirs, verbosity, test_filter = parse_cmdline() for element in dirs: import os, sys fn = os.path.splitext(os.path.basename(element))[0] if os.path.isfile(element): tn = 'test.test_%s' % fn else: tn = '%s.test' % fn os.environ['PYTHONPATH'] = '.' os.system(' ' + tn)

No code generation.

Just think about it for a while. What is the general purpose of all computer languages? To make a programmer’s life easier. What is the purpose of dynamically-typed, object oriented, dynamic programming languages, like Python, Ruby or PHP? To make a programmer’s life even easier. If it is so, a good framework should expose programming language’s features in a way, that should make code generation useless.

The source code is not for the computer. The source code is for you.

That’s why I won’t use Symfony. No, thank you. If it is well-thought and well-written, it should never ever require any automaticaly-generated code.