Simply Jonathan

Better url handling for web.py

In my post about web.py I touched upon the fact that I don’t like web.py’s way of defining URL’s, that I find it too loose. Well, it’s a minor problem, but rather trivial to solve, so I did. As quoted in the original post, there is a bit more typing, but I personally prefer this solution. (To be fair, the example I gave in the original post can be applied to this code as well, but I’d say this makes it a tiny bit more readable):

def webpyurl(_tuple):
    """
    Map a structured tuple to a url that web.py will accept.

    Turns a tuple of the format
    (
        ('a', 'b'),
        ('c', 'd')
    )
    into
    (
        'a', 'b',
        'c', 'd'
    )

    Rather simple, but can make a huge difference in readability.

    @require enhancedappend()
    """

    thislist = []

    for (a,b) in _tuple:
        enhancedappend(thislist, a, b)

    return tuple(thislist)

def enhancedappend(_list, *appends):
    """
    list.append only takes 1 argument, this allows for more to be added
    """

    for append in appends:
        _list.append(append)

This code is also available on pyhacker.

This is Simply Jonathan, a blog written by Jonathan Holst. It's mostly about technical topics (and mainly the Web at that), but an occasional post on clothing, sports, and general personal life topics can be found.

Jonathan Holst is a programmer, language enthusiast, sports fan, and appreciator of good design, living in Copenhagen, Denmark, Europe. He is also someone pretentious enough to call himself the 'author' of a blog. And talk about himself in the third person.