Archive for the 'Techblog' Category


Python’s Augmented Assignments 0

Everyone knows that in Python variables are just references and passing such reference to function effectively copies value of the reference to functions local scope, hence any assignment within function has no effect on original variable. Knowing that, this took me by surprise:


>>> def add_eggs(foo):
...     foo += ['eggs']
...
>>> spam = ['spam']
>>> add_eggs(spam)
>>> print spam
['spam', 'eggs']

This seemed to me as an error in Python interpreter, but after checking language reference I discovered it’s not; it’s intended behaviour (emphasis is mine):

An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.

Quoted after: python docs

Equality is not always sufficient 0

I’ve just got hit by one of really typical errors while writing simple value type in Python. Consider following code:

class Simple(object):
    def __init__(self, val):
        self.val = val
    def __eq__(self, other):
        return self.val == other.val

The class Simple overrides equality operator delegating it to it’s value. Seems dead simple and easy for the first time. But not quite so. What will following snippet print?

a, b = Simple(3), Simple(3)
set_a, set_b = set([a]), set([b])
print 'a == b: ', a == b
print 'set_a == set_b', set_a == set_b

You’ve guessed it: True, False. This is because two sets may be equal only if all elements hashes are equal. So I thought I will fix it with this:

class Hashimple(object):
    def __init__(self, val):
        self.val = val
    def __eq__(self, other):
        return self.val == other.val
    def __hash__(self):
        return (self.val * 5) + 7

Seems better, but look at this snippet and guess once more:

a, b = Hashimple(3), Hashimple(3)
set_a, set_b = set([a]), set([b])
print 'Hashimple'
print 'a == b: ', a == b
print 'set_a == set_b', set_a == set_b
print 'a != b', a != b

True, True, True?!? Well, Python will not imply that a != b , not even when a == b.

This bug has been made thousands times by thousands of coders, it occurs not only in Python, but also in various other languages (Java, Ruby), it’s been described in lots of books, but I just keep forgetting about it. Maybe writing a blog post will help me remember?

Docs: __eq__, __hash__

Idea about Expires header 10

Since the Steve Sounder’s High Performance Web Sites everyone knows to set far future Expires header on their static assets. What he didn’t say was how to deal with the file changes. You can’t instruct browser to download the file so you actually have to change the file’s URL.

Ruby on Rails handles this by automatically adding file’s timestamp as a query-string when using build-in helper methods — simple and not requiring server-side URLs rewriting. This is good enough, but only for files accessed from HTML directly and doesn’t work for files accessed from CSS: background images (unless you have dynamically generated styles).

I was thinking for some time about those background images, because they can be heavy and if you’re not using CSS sprites they make lots of requests. As I don’t like dynamically generated style sheets, I didn’t want any additional processing. And so it came to me: why not incorporate some changing value into the domain name? You could put a revision number or a timestamp in domain name of your statics server, just like this: static-1566.example.com. Both Rails and Django provide easy ways to change static assets domain.

This requires you to set-up wildcard subdomain, but is’s easier than processing CSS files. It works because url function in a style sheet is resolved using the style sheet’s own address.

PS: This is only a brain-dump — I didn’t test it.

Clean Code by Robert C. Martin 0

Clean Code is the best book I have read in last six months. In this book Robert C. Martin shares his great experience from the programming battlefield.

The book has a very good style. It guides you through refactoring of reams of Java code as if it was solving riddles. It’s not about high design, though, it’s about handling the code at a lower level: at the level of organizing classes, functions and blocks. It’s about writing a beautiful and readable code.

It’s also a book on craftsmanship. The author has emphasized this many times on his blog: being a craftsman-programmer means taking responsibility of producing good code only, just like being craftsman shoemaker, is all about making good shoes. And exactly the same way there are principles and best practices in both professions.

The uncompromising style helps the reader not to lose himself within arguments, but if you don’t like what Robert Martin says about certain principle, that’s very good (unless you can’t provide any arguments to back it up).

All in all it’s a very good book for anyone who wants to write better code. Get your own copy now and no worries! You’ll like it.

Bazaar with Master Branch 0

Problem statement

I have a bit svnish view of writing software: there should be one master branch for project just not to confuse users of your code where to get the most recent code from.

I’m writing a little app (I’ve posted about it earlier) with my girlfriend and we wanted to share and control code with possibly the best DVCS in the world. Bazaar promises an easy way for svn people to support central/master branch workflow. Or so I thought.

Early attempt

I was to confident with bazaar easiness. So I made bzr push over sftp to some directory on hauru.eu and told my girlfriend it’s ready. But it wasn’t: all files had my user permissions (uid/gid) and she couldn’t check anything in. So… It’s not so easy. I could change initial group for both of us, but this is not general solution (it wouldn’t work on WebFaction shared hosting where you can’t change initial group).

Heaving previous experience with setting svn central repository I tried to bzr serve the code. But this is simplistic mechanism with no read-write authentication support.

SGID to the rescue

With an advice from Paweł I tried setting SGID on repository directory. It sounded as a good idea, but it didn’t work. I was desperate, but I took another shot and browsed the web for solutions with SGID.

It seems there is bug in OpenSSH or python-paramiko or in bazaar sftp support and it doesn’t honor SGID properly. But with smart-server, that is bzr+ssh it does. Moreover it works much faster than sftp. So it’s double win.

Complete solution

In four steps:

  1. on server create empty repo folder
  2. change group to some common (users or bzr or your-project)
  3. set SGID on the folder
  4. now you can push your branch with
    bzr push bzr+ssh://server/repo-path
    

Every one in group can check code in and out.

Of course this should not stop you from having as many side branches as you want, but it's good to have a single code branch you can always refer to.

Next Page »