Posts for the month of October 2009

Worin liegt der Erfolg von "Ruby on Rails"

Liegt es vielleicht wie  auf Stackoverflow angekündigt an der Sprache?

Jedenfalls fand ich den ersten Kommentar hervorragend.

  "Python on Pails" just doesn't have the same feel to it... – ephemient
  @Ephemient: I believe it would be Python on Planes. – Jimmy
  @Jimmy: Who needs planes? import antigravity ;-) xkcd.com/353 – Vinay Sajip
  Is there a Java in Jails? – Nosredna

*lol* Eindeutig, die Sprache ist schuld.

  • Posted: 2009-10-24 10:42 (Updated: 2009-11-04 10:07)
  • Author: dwt
  • Categories: funny
  • Comments (0)

Playing with JavaScript variable lookup

I am always amazed at the very nice features of JavaScript on the one hand and the very, very bad features of it on the other side.

Here's something I learned the other week that I find quite interesting: eval vs. new Function. Here's what I wanted to achieve: I was looking for a way to do some meta-programming with JavaScrip, specifically prevent the problem that any variable you assign to but don't declare ends up as a member of the global object.

The problem is that name lookup in JavaScript is quite peculiar. First it consults the current function activation for local variables and then it goes straight back to the window object and just prepends all it's contents to the local namespace.

Doh. This has the very bad consequence that every variable you forget to declare via the var someVariableName syntax becomes part of the global object - and therefore itself global.

Now you can change this lookup by inserting some of your objects in this lookup chain by using the  somewhat controversial with statement like so:

var namespace = {foo:'bar'};
with (namespace) {
    // this scope now has a local variable foo with the value bar
}

This is considered a bad feature, as it means that if you assign to foo that will assign a new value to namespace but if you assign to something else or mistype, that will still end up on the global object. Not very nice - and therefore most JavaScript programmers don't use with ever.

Still with some working and eval it can be used to re-map free functions transparently, so that you can do something like this:

var namespace = {
    equals: function(actual, expected){
        if (actual !== expected)
            console.log('actual', actual, 'does not equal expected', expected);
    }
}

function test(aDescription, aTestFunction) {
    useNamespaceForFunction(namespace , aTestFunction)();
}

test("that I can call equals as a free function", function(){
    equals(1,1);
});

Which would allow you to not pollute the global namespace with all the testing equality functions but still call them in a convenient way (that is without needing to go through an object.

All in all, quite nice really - even though I haven't really found a use for this yet. :-)

Implementing the useNamespaceForFunction however isn't quite as straightforward as I had thought - here's my first go at it:

function useNamespaceForFunction(aNamespace, aFunction) {
    with (aNamespace) {
        return  eval('(' + aFunction + ')');
    }
}

I have since learned that actually using new Function to do the eval might be quite a good idea as the whole point of it is that it ignores the namespace around it, so here's my version two:

function useNamespaceForFunction(aNamespace, aFunction) {
    var namespacingCode = "with (aNamespace) { return (" + aFunction + "); }";
    // using new Function instead of eval to prevent the current namespace leaking into the eval<
    return new Function("aNamespace", namespacingCode)(aNamespace);
}

So, lets see what uses come up for this technique - I've seen some js code that does an Interpreter of sorts with this trick - but thats about it.

So - I didn't achieve my initial goal - but I did come nearer to it. So I'll call it a success here for now.

If you find any use for this technique, please let me know!

Get the source!

  • Posted: 2009-10-17 18:11 (Updated: 2009-10-24 16:02)
  • Author: dwt
  • Categories: code
  • Comments (0)

Simple super

Python is a wonderfull language - except when it's not.

For example calling a super-method is really, really hard. Here's an example:

class Super(object):
    def method(self):
        pass

class Sub(Super):
    def method(self):
        super(Sub, self).method()

This is bad because of several reasons:

If your class names become longer this becomes more and more unreadable, consider this Acceptance-Test

class CanEnterUsernameAndPasswordOnLoginForm(TestCase):
    def setUp(self):
        super(CanEnterUsernameAndPasswordOnLoginForm, self).setUp()
        # more

You need to repeat the class name in each method that calls super. This is especially bad if you rename your class as you need to repeat the name in so many places - also there might be situations where having the wrong name doesn't bomb but just calls the wrong code. Also if you move methods up and down the class-inheritance-chain this becomes more and more annoying.

Well, so I looked at what you can do with some meta-programming - and lo and behold there's a lot you can do.

Here's an example what I ended up with:

class Super(object):
    super = SuperProxy()
    def method(self):
        pass

class Sub(Super):
    def method(self):
        self.super()

Yeah! Now that's simpler. to call super you can use several syntaxes:

  • self.super() just calls the super method of the same name and hands it all arguments that the current method got
  • self.super('foo') this way you can hand specific methods to the super-class and prevent the automatic gathering of arguments. If you prefer explicit - this is it.
  • self.super.some_method() self.super is exactly the same as what the super-method returns (so it's the same as super(ThisClass, self)) so you can use it to call any method on the superclass and hand it any arguments you want.

Well, so I consider this a boon for any bigger python project as it considerably eases the pain of working with class-hierarchies, and best of all you can import it into your project one superclass at a time.

Oh, and please tell me if you use it and like it. :-)

So here's the code!

  • Posted: 2009-10-14 20:49 (Updated: 2009-10-14 20:50)
  • Author: dwt
  • Categories: code
  • Comments (0)

Demokratie ohne Parteien

Die Welt hat ein  Interview mit Juli Zeh geführt, in der sie darüber spricht, was sie gerade als staatstheoretisches Konzept ausarbeitet - und wie darin Parteien nicht vorkommen.

Das ist spannend - insbesondere weil ich mich im Rahmen  der Piratenpartei und  des Liquid Democracy e.V. und eben auch privat seit Jahren mit genau diesem Thema beschäftige.

Julis (und auch meine) Motivation ist dabei, dass die Parteien als "vorportioniertes Politikbündel", das man so fressen oder es lassen kann, inzwischen mehr zurückhaltende als vorwärts tragende Kraft sind.

Da ist zum einen der Zwang, dass ich mich für ein Politikbündel irgendeiner Partei entscheiden muss, wobei aber jedes Bündel für sich in mehr Punkten meiner Meinung widerspricht als ich es unterstützten kann.

Zum anderen die Tendenz, dass Parteien einen immer stärkeren Fokus auf Machterhalt, Personen und Einflussnahme von reichen Industriezweigen entwickeln.

Genau das aber will Juli - und ich - nicht. Demokratie als sachlicher Diskurs unter Einbeziehung allen verfügbaren Wissens und ohne Populismus - das ist eine Vision, die jetzt vielleicht endlich durch die neuen Möglichkeiten des Internets in erreichbare Nähe gerückt ist.

Worum es mir geht ist das Nachdenken über neue Formen der politischen Partizipation.

Wir sehen in der Struktur der Legislative das eigentliche Problem und wollen diese ändern.

Jeder Bürger soll die Möglichkeit erhalten, an so vielen politischen Fragen mitzudiskutieren und mitzuentscheiden, wie er möchte. Das nennen wir "Flüssige Demokratie", weil jeder jederzeit selbst entscheiden kann, wie tief er sich in ein Thema einbringen will und kann.

Wenn mich etwas nicht interessiert, kann ich weiterhin eine Partei wählen, damit sie mich vertritt. Wenn ich zu einem bestimmten Thema (z.B. Bundeswehreinsatz in Afghanistan) selbst abstimmen möchte kann ich das aber jederzeit tun. Und wenn es darum geht mein Fachwissen (z.B. als Informatiker) in die Umsetzung eines Konzepts einzubringen (z.B. zur Frage, wie man die Verwaltung besser transparent machen kann) dann steht es mir frei, in der Konzeptionsphase schon mit einzusteigen oder selbst ein Konzept zu entwickeln und an der ganzen Entstehung mitzuarbeiten.

Das ist eine Vision, die weit über alles hinausgeht, was mir als Konzept bisher begegnet ist. Vor allem aber ist es genau dieser flüssige Ansatz, der dieses Konzept in die Lage versetzt, nahezu unendlich zu skalieren. Mein Dorf, mein Landkreis, mein Land, mein Kontinent, meine Welt - überall kann ich mich dort beteiligen, wo ich es als am Wichtigsten erachte.

Nur als Nebensatz: Ich bin der Meinung dass ein bedingungsloses Grundeinkommen eine hervorragende Bezahlung für genau diese freiwillige demokratische Arbeit ist und wir es daher dringend brauchen.

Mehr Details zu dieser Idee gibt es auf:  http://liqd.de und auf allen anderen Links in diesem Artikel.

Ich bin Martin Häcker, ich bin Vorstand der Piratenpartei Landesverband Berlin. Wir haben bei der letzten Bundestagswahl 3,4 % der Stimmen der Berliner erhalten. Und wir wollen das trojanische Pferd sein, das diese Vision in die Parlamente trägt, indem wir es innerhalb der Partei einsetzen, um unser Programm gemeinsam - wirklich basisdemokratisch - zu erarbeiten.

  • Posted: 2009-10-03 22:21 (Updated: 2009-10-03 23:26)
  • Author: dwt
  • Categories: politik
  • Comments (0)