Python pregenerates class properties

This article is a reference because this cost me about an hour to debug:

class test(object):
foo = []

a = test()
b = test()
a.foo.append("bar")
print b.foo
#['bar']

When you define the class (test, in this case), any class properties will be instanciated on class-definition and not on object instanciation. Therefore, when you create multiple copies of a class, they will have the same actual object as class properties. You should instanciate those attributes in the init-method.

For this reason, the following code will generate output, even though it looks like it probably shouldn’t, because not a single object was created:
def f():
print "foo"

class test(object):
foo = [f()]

This will obviously not concern you, if you are – as you should – using immutable objects.

Syntactical sugar in Python: map, filter, reduce vs. the python way

This article is going to try to shed some light on how to convert map, reduce and filter (often used with lambda expressions) in python into a syntax conforming to “the python way”. At the bottom you will find a download link to execute this stuff for yourself.

l = range(10)
print l
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This is going to be the list (l) we’re working on.

What we’ll be looking at first, is iterating through this list and applying a function. And what simpler function could there be, than the identity?
a = map(lambda x: x, l)
b = list(x for x in l)
print a, b
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If you don’t really know what the we did in (a) you may want to read up on lambda expressions, a topic I am not covering now. We need the list-method in (b) because map() always returns a list and the for-expression coughs up a generator object which we have to fully evaluate and convert to a list, to get the equivalent. Generator objects I will cover in the future. For now, suffice to say, it has a reason we are applying the list-function.

Weiterlesen

The php comparison game

Recently, I had a discussion with EnnoBigBenno, one of the friends over at senaeh.de about the merits of php (or lack thereof). I believe that a good and simple programming language can easily be parsed and understood by a human. This brought me to the idea, to create a small game of “True or False” where you have to decide, what the php-interpreter thinks, is true or false. Simple as that. Count your correct answers and check you php-prowess.

Warning: This game is not for the faint of heart. And feel free to share your score in the comments.
Weiterlesen

How I envy thee

A few months ago, I was at a friend’s party. As the evening progressed, an old colleague arrived with his girlfriend. And not only did she have the most magnificent breasts, she was also blessed with the mind of a fourteen-year-old, as well as the eloquence of the younger sister of said fourteen-year-old.

When they arrived, he already was of the drunken persuasion and she, as stupid she seemed, was kind-eyed enough, to see more in him then the drunkard we all felt he were.

As the party progressed, our drunken friend seemed determined to getting to a state of not knowing how he will have gotten home. While his company was sure to, as she always does, safely deliver him home, wherever he may end up in the course of a – at least to them - usual saturday evening.

And after an amount of beer that drinking seemed like a superhuman feat, he slipped into the bed of one of the hosts and fell asleep at the spot. As I wasn’t nearly as content with my human company as he was with his bottle-shaped companions, I sat idly on the sofa at the far side of the room, soon being joined by another guest who, as I, didn’t like the sound of uncalled for live guitar music.

And as we sat there, him staring through the bottom of his glass, me contemplating suicide, I thought about how happy life could be, if we were just drunk and sound asleep. Ignorance is bliss and how blissful we could live, if we could just shed this bad habit of thinking about the consequences of our actions. How easy it would be, how life could be lived to the fullest, if we stopped considering other people and what they would call “their feelings”. How I envied him, not knowing and more importantly, not caring how angry the bed-owner was soon about to be about his intoxicated intruder.

And now I am stuck here with my glass of remedy, deciding whether I should tell of me, probably being in the right and maybe even having it my way and possibly hurting somebody, or shut my mouth and look the fool, chances are I am.

Why do actions always have consequences? And more importantly, why do I have to weigh those actions by them?

“Standing behind Shit: The Game” – Two days of Path of Exile

My short summary of about two days of Path of Exile:
You’re always standing behind shit.

Using sass with html5 boilerplate

This article is about how easy it is to use Sass with the html5 boilerplate.

So you installed Sass, which, on Fedora, was as easy as

sudo yum install rubygem-sass

but you will find your way in Sass’s documentation.

Then you downloaded html5boilerplate (from initializr.com) and the ant buildscript. Unpack your html5 boilerplate into a folder and unpack the buildscript in the subdirectory “build” so that your directory structure looks something like this:

.
|-- build
|-- css
|-- img
|-- index.html
|-- js
`-- robots.txt

now you edit the file “build/config/project.properties” and uncomment the line “build.css.scss            = true” (which was line 42 on my system) and you are done.

Now you can use Sass in your main.css wherever you want and the build script will compile it to valid css. Your browser does not understand Sass so you can no longer test your site without building it. You should use the basics buildtarget for ant to generate your css code for testing (and not optimizing images and such):

cd build
ant basics

You’ll find the compiled version in your “publish” subfolder where you can look at your changes.

Taking more of the same too

There are more films coming where a bad actor’s daughter gets taken.

But this one seems like it might be fun. I think Stalone is way too bad an actor to take himself very seriously and I like the idea that he does what needs to be done and the cop with him can do nothing because he needs it done as well.

I see lots of comedic potential. Will make for an average but lighthearted movie. I approve.