vmx

the blllog.

pythonpath: access nested data structures easily

2008-11-08 22:35

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

Comments are closed after 14 days.

By Volker Mische

Powered by Kukkaisvoima version 7