Django – sharing a memcached instance

Update: Some Curious User brought to my attention, that a ticket has been opened which, when implemented, will add a setting for a cache prefix. It will also allow other cache key manipulations. Django has implemented KEY_PREFIX in the development version, which currently means, that it will be out in 1.4 iirc. Django 1.3 has implemented KEY_PREFIX which solves the problem once and for all.

Until recently I’ve been using the file:// django cache, but that has a “problem” when multiple users needs to manipulate the cache (think uid 80 writes a key, that uid 1000 wants to delete).

My problem with the memcached:// django cache provider has been, that it cannot handle being used on a shared memcached instance, because of the danger of key collissions.

Continue reading

Python http_head method

Seeing as there is no really easy way to do a HTTP HEAD request from python, I wrote up the following small method:

In advance I’d like to apologize for the method that assemblies the request path.

Update: Added handling of redirects.

def http_head(url):
    import httplib
    import urlparse
 
    redirects = 0
 
    while redirects < 10:
        scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
 
        if scheme == 'https':
            conn = httplib.HTTPSConnection(netloc)
        else:
            conn = httplib.HTTPConnection(netloc)
 
        conn.request("HEAD", "%s%s%s%s%s" % (path, query and "?" or "", query,
                                             fragment and "#" or "", fragment))
 
        res = conn.getresponse()
 
        if res.status in (301, 302) and res.getheader('location'):
            url = res.getheader('location')
            redirects += 1
        else:
            break
 
    return res.status, res.reason

Very simple email sending method in python

I needed to send an email, so I came up with this:

 
def send_plain_mail(subject, body, from_mail, to):
    import smtplib
    from email.MIMEText import MIMEText
    from email.Encoders import encode_quopri
 
    msg = MIMEText(body, 'plain', 'iso-8859-1')
 
    msg['Subject'] = subject
    msg['From'] = from_mail
    msg['To'] = to
 
    s = smtplib.SMTP()
    s.connect()
    s.sendmail(from_mail, [to], msg.as_string())
    s.close()

Not rocket science, but it gets the job done.

Django compatible PyAMF test client

While working on a project using PyAMF today, i was about to write a unittest of a service method, when I realized that it would be harder than necessary to make unittests of the service. The cause of this is, that there was no test client wrapper for the django test client for pyamf, which means, that to do a unittest of the gateway, you’ll have to start a django server, and run a unittest elsewhere.

Seeing as how there are a lot of benefits to using djangos own test suite (fixtures and automatic database generation to name a few), i set out to write a little test client for PyAMF to utilize django’s test client, so it would be possible to write a proper test suite.

This turned out great, and is now ticket 508 over at PyAMFs trac. Look at the client.py for the code. At some point, it will be integrated into PyAMF mainline as p.r.c.django.TestClient (or something like that).

Choosing a font in Carbon Emacs

It took me a while of messing with fontsets and various other settings, before i came across how to select fonts in the Emacs wiki:

  1. M-x mac-font-panel-mode and pick the font you want.
  2. M-x describe-font and copy the font name (e.g. “-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1”)
  3. Add the following to your .emacs file: (set-default-font "-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1")

In my case the lines for font selection looks like this:

(set-face-font 'default "-apple-proggytiny-medium-r-normal--11-110-72-72-m-110-iso10646-1")

Using this to make the font display without antialisaing:

defaults write org.gnu.Emacs AppleAntiAliasingThreshold 100

And this to position emacs in the top left, and be 104 chars wide, and 64 high.

(setq initial-frame-alist `((width . 106) (height . 64)
			    (top . 0) (left . 0)))

And of course some other tweaks. Re-learning emacs is turning out to be a good idea. But that’s another story.

A ongoing discussion about C and libmemcached – don’t censor me

And there I was, happy as a 4-year old who had just found a whole box of strawberries.

Then all of a sudden, along came a post about how Amir Salihefendic had made a little daemon in C, using libevent and memcached, that would allow him to cache a http request in memcached. I don’t quite get why one would like to reinvent a reverse http proxy like that, and stated my arguments on why I thought that this seemed like a really bad idea.

As you can tell from the comments, he did not agree. It ended up with him deleting my last comment. Good thing I kept a copy in my clipboard. So, here are the comments. Continue reading