Label : python

ipython classic mode / precise pangolin

By | | Tech | Schrijf als eerste een commentaar!

The Ubuntu do-release-upgrade broke my ipython classic mode. The ipython package was upgraded, and apparently the configuration parser was changed.

In bash, I want colors to help me find the beginning and end of output -- see this bug report for others agreeing with me that the derogatory comment about "focus should be on the output, not on the prompt" in the skeleton .bashrc is is retared, but I diverge -- in ipython, I just want to see the nice >>> blocks that I'm used to and no extra spaces. I.e.: classic mode.

Previously, one could set classic 1 in ~/.ipythonrc ...

python virtualenv / global site-packages

By | | Tech | Schrijf als eerste een commentaar!

If you're switching from Ubuntu Oneiric to Ubuntu Precise and you're using python-virtualenv, you might be in for a surprise:

The default access to the global site-packages modules is reversed between virtualenv 1.6.x and 1.7.

When you were used to finding your apt-get installed python modules like python-mysqldb and python-psycopg2 in your new virtualenv environment, now they're suddenly unavailable.

The culprit:

       --no-site-packages
           Ignored (the default).  Don“t give access to the global
           site-packages modules to the virtual environment.

That was not the default before.

The new option to get the old behaviour:

       --system-site-packages
           Give ...

mysql slow / queries / sample

By | | Tech | Schrijf als eerste een commentaar!

Sometimes you're in a situation where you know that a database is more heavily loaded than it should be. Time to figure out which queries are stressing it the most.

The standard thing to do with a MySQL database would be to enable query logging with general_log_file. Or, to get only slow queries and those not using indexes, the log_slow_queries.

But, if this is a mission critical and heavily loaded database, adding expensive logging may be just enough to give it that final push to become overloaded.

Perhaps taking just a sample of queries is good enough too. In ...

django / mongodb / manage dbshell

By | | Tech | Schrijf als eerste een commentaar!

The current django-mongodb-engine doesn't seem to ship with a working manage dbshell command yet. Right now it returns this:

$ ./manage.py dbshell
...
  File "/home/walter/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/management/commands/dbshell.py", line 21, in handle
    connection.client.runshell()
  File "/home/walter/.virtualenvs/myproject/lib/python2.6/site-packages/django_mongodb_engine/base.py", line 108, in __getattr__
    raise AttributeError(attr)
AttributeError: client

The fix is simple, patch your django_mongodb_engine with this:

--- django_mongodb_engine/base.py.orig  2011-11-15 11:53:47.000000000 +0100
+++ django_mongodb_engine/base.py 2011-11-15 11:54:07.000000000 +0100
@@ -7,6 +7,7 @@
 from pymongo ...

django / query expression / negate

By | | Tech | 2 commentaren

Suppose you have an is_enabled boolean in your Django model.

class Rule(models.Model):
    is_enabled = models.BooleanField(blank=True)
    # other exciting fields here

And now imagine you want to negate the is_enabled values. Something you would easily do in SQL, with: UPDATE myapp_rule SET is_enabled = NOT is_enabled;

The Django F-syntax is nice, and looks like it should be up for the task.

Let's sum up a couple of attempts:

Rule.objects.update(is_enabled=(not F('is_enabled')))

No! You get this:
UPDATE myapp_rule SET is_enabled = true;

Rule.objects.update(is_enabled=(True ^ F('is_enabled')))

No! You get this:
unsupported operand type ...