Welcome tour in the wiki

It’s no secret that around here we are big wiki fans. But the paradox of wikis is that it’s so easy to collect information that suddenly there is too much of it. And that makes it hard to navigate the information. So a wiki must constantly be refactored - especially the navigation structures. But no matter how good your navigation structures are, how do you make it easy for newcomers to get into the wiki? Our main space at Nektoon contains 452 pages at the moment - that’s after only six months of existence. At local.ch, my former work, there is a lot more after several years of wiki usage with many more people involved. ...

July 15, 2009 · Patrice Neff

Pot: namedtuple

For returning complex values from a method you have a few popular choices: Return a tuple with positional values. Those are very easy to unpack in Python and quite attractive because of that. Return a dictionary with the values. This can lead to slightly verbose accessing of the individual values. Return an object of some class you define. This required the definition of a class which can be too verbose for just a simple return value. Since Python 2.6 you have one additional tool: collections.namedtuple. They give you a very lightweight class but it’s a one-liner to define them. ...

June 7, 2009 · Patrice Neff

Modular Python with eggs

I did a presentation at the March swiss.py user group meeting about using Python eggs. I hear a lot of bitching about Python’s packaging. Guido even managed to talk at lengths about the problems with it at his PyCon keynote talk. Here I am boldly saying almost the opposite: Python packaging rocks! It’s very easy to have a great workflow both for development as well as deployment using Python eggs. Some notes from the presentation which you can download as a PDF. ...

April 15, 2009 · Patrice Neff

Autofill birthday fields in Safari

The Safari web browser can autofill form fields from your personal address book card. I’ve had very good experiences with this feature. But today I was creeped out a little on my brightkite profile as I saw my birthday even though I had not provided it anywhere. It took me a moment to realize that Safari had autofilled my birthday. So I quickly came up with an evil idea. Would it be possible to abuse that feature to automatically collect birthdays from users on say a signup form? ...

April 9, 2009 · Patrice Neff

Pot: nose

nose is my Python testing tool of choice. You can use it to very easily create test suites. While it does support the unittest module which is included with Python, it also allows for a much simple style using just methods and asserts. Put the following code into a file called test_basic.py: <notextile>def test_addition(): print 2 + 2 assert 2 + 2 == 4 def test_addition_wrong(): print 2 + 3 assert 2 + 3 == 6</notextile> Then just run nosetests in the directory where you stored this file. You’ll get an output indicating, that there is a test failure. ...

April 6, 2009 · Patrice Neff

swiss.py #2

Tomorrow (March 31) we’ll hold the second swiss.py user group event. We’ll meet at the main ETH building at 19:30. If all goes well, Florian Bösch will talk about OpenGL development using Python. He’s currently not feeling that well, so the topic might change on the day itself. Get more details on the official swiss.py web site.

March 30, 2009 · Patrice Neff

Autorefresh for local.ch location pages

The local.ch location pages contain many nice information about every city and village from Switzerland. Since putting the regional news online, I regularly look at the location page for Olten. I’ve now created a Greasmonkey script which - once installed - automatically jumps from tab to tab every five seconds. You can get the script. When you open that page Greasemonkey will ask you to install the script. I’ve tested the script both with the original Greasemonkey as well as with the Safari implementation GreaseKit. ...

March 23, 2009 · Patrice Neff

Pot: the with statement

Python’s with statement can be a very elegant alternative to long try/except/finally clauses. It offers a standard protocol that classes can implement to properly clean up state. The best example for it’s value is file reading. A good implementation would have to look like this traditionally: f = open('/tmp/myfile', 'r') try: content = f.read() finally: f.close() The with statement shortens this to the following code: with open('/tmp/myfile', 'r') as f: content = f.read() Behind the scenes it wraps the finally statement around this and makes sure that the file gets closed upon leaving the with block. ...

March 19, 2009 · Patrice Neff

Pot: Class properties

As most programmers, I’ve been trained with the getter/setter idiom. It allows a programmer in any object-oriented programming language to create a class interface which makes sure that only valid data is written. An example of a class with getter/setter in Python: class Person1(object): def __init__(self): self._age = None def get_age(self): return self._age def set_age(self, age): self._age = age We just wrote two methods - four lines - only to be able to modify and access some data. And we have to do that for every property. This is such a common idiom, that IDEs even have wizards for that. ...

March 12, 2009 · Patrice Neff

Pot: String formatting

Starting with Python 2.6 there is a new format function to do string formatting. I’ve always been a bit overwhelmed by the official Format String Syntax documentation so I’ll try to present it to you with more examples. Python so far had the ‘%’ operator. It’s been deprecated with Python 2.6 and removed from Python 3. Instead string has a new format method. >>> "This is an %s-style formatting" % "old" 'This is an old-style formatting' >>> "This is a {0}-style formatting".format("new") 'This is an new-style formatting' Formatting options go after a colon, for example the width specifications together with alignment options which are mostly useful for tabular representations. Let’s print the following table as an exercise: ...

March 5, 2009 · Patrice Neff