vmx

the blllog.

Filmtipp: Man on Wire

2008-11-09 22:27

Wenn ich schonmal einen Film im Kino gesehen habe, der nicht schon vor Wochen/Monaten in Deutschland gelaufen ist, muss ich einfach darüber bloggen.

Es handelt sich dabei um den Dokumentarfilm Man on Wire (IMDB), der über die Hintergründe des Hochseilaktes zwischen den Türmen des World Trade Centers berichtet. Keineswegs unverdient hat er beim Sundance Festival 2008 den Grand Jury Prize und Publikumspreis abgeräumt.

Viel zu sagen gibt es nicht, außer dass es ein außerordentlichen Film ist, der es schafft mit einer unglaubliche Geschichte, einem wahrgewordenen Traum, Gefühl und Leidenschaft direkt in den Herzen der Zuschauer zu landen.

Categories: de, Kino

pythonpath: access nested data structures easily

2008-11-08 22:27

With pythonpath you can access values within nested Python data structures easily. This is especially useful if you use JSON. If know which element you want, you can access it directly with a string (a pythonpath).

I created pythonpath for GeoCouch as I needed a way to point to a specific element within a JSON structure. But I think it might be useful for others as well, hence this blog entry.

Take the following data structure:

{ "type": "Feature",
  "geometry": {"type": "Point", "coordinates": [151.21 -33.87]}
}

If you want to get the type of the geometry, you'd normally access it by

type = geo['geometry']['type'].

With pythonpath it would be:

type = pythonpath.get_item('geometry.type', geo)

Accessing array elements is just as easy:

lat = pythonpath.get_item('geometry.coordinates[1]', geo)

The syntax is quite simple, a dot (.propertyname) for dictionary element, array notation for an array element ([position]). The escape character is backslash (\).

Here's the code:

# Copyright (c) 2008 Volker Mische (http://vmx.cx/)
# Licensed under MIT.

import operator
import re

def parser(path):
    items = []

    for prop in re.split('(? 0:
            if len(prop) > len(brackets):
                items.append(prop[:-len(brackets)])

            for index in re.findall('\[(\d+)\]', brackets):
                items.append(int(index))

            continue

        items.append(prop)

    return items


def get_item(path, data):
    itemgetters = map(operator.itemgetter, parser(path))
    for getit in itemgetters:
        data = getit(data)
    return data

You can also download this code together with some tests.

Categories: en, Python

By Volker Mische

Powered by Kukkaisvoima version 7