Stavros' Stuff

Angry rants of programming and other things.

Django's per-site caching doesn't work

Surprise! Your cache doesn’t.

A few days ago, I wrote a post about a peculiar piece of code that a friend of mine had sent me. Since it was interesting bit of code, I thought Hacker News would enjoy it, so I posted it there. To my great pleasure, the post shot up to the first place in a few minutes and continued there for a full day, bringing just over 50,000 visitors to this blog, in total.

I was very happy that people were liking and discussing this post (and the discussion was very interesting in its own right), but I noticed that AppEngine, where this blog is hosted, was struggling to serve it. I had to create new instances because the average latency was about ten seconds(!), even though this blog is pretty much only text and static media, and I use Django’s per-site cache to cache every single page.

Continue reading…

Brilliant or insane code?

A Moste Wonderfull Tale of Optimizations and Legibility

I came upon a very interesting and cryptic snippet of code somewhere nameless, and I can’t decide if it is brilliant or completely insane. It is a very obscure way of accomplishing the required task, but it’s around four times faster than the alternatives I’ve tried, so I have to admit that it’s not completely without merit. Still, I cringe a bit at seeing it, since it packs around four unusual Python concepts in almost as many characters.

This is the snippet in question:

Continue reading…

Faster installs with pip

Today’s tip is brought to you by the color green and the number g.

If you’re an industrious Python programmer, you probably use pip and virtualenv to install your packages in an isolated location, which is great, and good job to you. However, having to install Django, pandas and ipython every time you want to experiment with something is a bit tiresome, since they’re all big files and downloading them takes a while, plus what if you’re on a data-limited connection and bla bla bla.

Here’s a very easy way to both speed up your downloads and avoid having to do them in the first place:

Continue reading…

Django template projects

If you haven’t heard yet, let me break the good news to you: Django 1.6 is getting template apps/projects. This means that, when starting a new project, you don’t have to copy your old directories and change stuff around any more, you can just put them into reusable directories (or even repositories) and Django will be able to use them very simply:

django-admin.py startproject --template=/djtemplates/default myapp

I’ve been needing a feature like that for ages, and now I’m giddy with excitement, so, without further ado,

Continue reading…

Monitoring your system's temperatures with Python

I have suddenly noticed that my computer’s fans have been more noisy than when I first bought it, and now it’s driving me crazy. I wondered if I could cut the fans’ power without increasing the temperatures inside the case too much, or even leave the case open. To test it, I spent five minutes writing a small script to monitor temperatures in Python, using smartmontools and lm-sensors.

This is that script:

Continue reading…

How to replace a Django model field with a property

If you use Django for your web apps, you will probably have come across a situation where you need to run some custom code on assigning a value to a model field. You’ve probably hacked around this by overriding the save() method or some other arcane hackery, but this is not only unnecessary, but it’s more complicated than it should be.

You can use a Python property to add a getter/setter to the field and run your custom code there. Even better, you don’t even have to…

Continue reading…

How to highlight code in Python

I’ve recently been looking at syntax highlighters, both for this blog and for Instahero, the new product my company is developing. I used to use a JS-based solution for the blog, but it broke with many inputs, it was fiddly to work with, didn’t parse correctly sometimes, and slowed the site down considerably. I especially disliked the fact that you had to include one JS and one CSS file per language you wanted to highlight, which either significantly constrained the highlighting that could be done easily, or increased the size of the page (and…

Continue reading…

The curious case of the range() function

If you’ve ever programmed in Python, you have a 96.2% chance of having used the range() function. As you have a 96.1% chance of knowing, said function returns a list containing an arithmetic progression of integers, exactly as the documentation says. For example, range(4) returns [0, 1 2, 3].

If you’ve ever bothered to look at the docstring, you will see that it goes thus:

Continue reading…

Developing and deploying Python apps using pip and virtualenv

This is one of a series of tutorials that started with the popular Learn Python in Ten Minutes. The entire series is available as an ebook, which has all the tutorials in one convenient, DRM-free format, and will get any updates with later tutorials for free.

So, you’ve written an amazing Python program, it does everything you want and then some. You stop and stare at it in amazement, and realize that it’s just so great, that it would be selfish not to share it with the entire world.

However, there’s one small problem. Your program uses…

Continue reading…

A schemaless layer over SQLite

I’ve been hard at work these days, and to relax I decided to develop a small application/side project. As with many applications, I needed a reliable, persistent store, so I went with the most reliable, easy to set up, performant, and generally awesome store I knew: SQLite.

However, since this was a very experimental prototype, I got tired of the frequent schema changes and wished I had something that didn’t dictate a schema, but that I could still query quickly. MongoDB is, theoretically, a very good fit for this, but I’m not very fond of it…

Continue reading…