Skip to main content.
April 30th, 2007

Theme-ing

Ok, I wrote the template of my blog the way I liked. I didn’t consider I would have used wordpress, and this showed out. So, got the first theme that give me some good vibes and Real Simplicity ™, and used that.

One day RedFrame will be out, and I’ll use that. One day.

Posted by mattia as wordpress at 6:41 PM CEST

No Comments »

April 21st, 2007

LVM, XFS and boot remake adventure

New Ubuntu version, Feisty Fawn, and I decide to upgrade. A little problem: my /boot is too little. I won’t tell you how little I did it because I’m not that proud of my choice, now. Anyway, I have a 4-disk system managed by LVM, with a single XFS partition over it. luckily, one of them was spare (I planned to put windows on it, then I didn’t need it and was yet free), because XFS can only be grown, never shrinked (funny choice, by the way). So, I had to create a new boot partition, migrate the contents there, and make the old boot partition join the LVM. Since this involved something delicate (yes, that plain, old boot partition) I did things with care. Not enough, since I forgot one thing and had to put the server up again making things locally. Anyway, if you’re brave, you can technically make all that I talked about with your system online and running. How? This is the roadmap:

So, here’s the end, and after that I could update to Feisty with its funny I-do-it-all-myself update manager. End of the Dilbertian Space.

Posted by mattia as boot, lvm, xfs at 10:30 PM CEST

No Comments »

April 19th, 2007

Law of the retrocompatibility

Retrocompatibility is not-bug-prone and meaningful only on good software.
Nobody writes good software.
Thus, retrocompatibility is always meaningless.

Wow.

Posted by mattia as fun at 1:43 PM CEST

No Comments »

April 18th, 2007

misteries of python’s lambda function

I have a Python’s cycle into which I would like to make lambdas:

s = {}
for x in ['stupid', 'genial']:
s[x] = lambda prefix: "%s %s" % (prefix, x)


print s['stupid']('I am')
print s['genial']('You are')

Would you expect this output?

I am stupid
You are genial

No, no. This is the real output:

I am genial
You are genial

Even if I’m happy to be genial too, I wonder what happened. The reply is simple: python’s lambda binding. The x variables isn’t bound when the lambda is constructed, but when the lambda is called. Since both are called only at the end of the cycle, its value is the last value of x, which is 'genial'. Ok, now that we’ve understood what had happened, how can I solve this problem?

If you’re lucky, you’re using Python 2.5 or higher. In this case, they invented functools, and functools.partial:

from functools import partial

s = {}
for x in ['stupid', 'genial']:
  s[x] = partial(lambda prefix, xx: "%s %s" % (prefix, xx), xx = x)

print s['stupid']('I am')
print s['genial']('You are')

Now the lambda takes two arguments, one of which is bound, forceably, to the current value of x. Ok, this solution is not so elegant, but at least is a one-liner. And if you have a lower version of Python? Have fun in decyphering the following one:

s = {}
for x in ['stupid', 'genial']:
  def aux():
    v = x
    return lambda prefix: "%s %s" % (prefix, v)
  s[x] = aux()

print s['stupid']('I am')
print s['genial']('You are')

It works on a simple trick: the aux function put the too inconstant variable x in a local variable, v, then use that one. Since a new local variable is allocated every time, the value of x is saved in that local variable. It is thrown away, once the function exit, but the lambda-binding save the variable, which we can safely get afterwards.

I quite love Python, but, really… some things are really stupid!

Posted by mattia as functools, lambda, python at 2:01 PM CEST

No Comments »

« Previous Entries