ANN: a mini-language for encapsulating deep-copy operations on Python data structures

M.-A. Lemburg mal at
Wed Nov 18 13:34:14 CET 2009

Steve Howell wrote:
> During the last few days I have written code in support of a small DDL
> language that encapsulates a concise representation of the
> manipulations needed to make a deep subcopy of a Python-like data
> structure. It is inspired by syntax from mainstream modern languages,
> including, of course, Python. The DDL can be converted to an AST. That
> AST can be walked to either generate Python code that expresses the
> mapping or to generate Python objects that can execute the mapping.
> Either of the prior outputs can then subsequently be applied to real
> world inputs to create new Python data structures from old ones, using
> the mechanisms specified in the original DDL for attribute access,
> dictionary lookup, iteration, method invocation, etc.
> Here is an example of the DDL (and I hate the terminology "DDL," just
> cannot think of anything better):
>             {
>                 'show_table_of_contents',
>                 'author' {
>                     .person 'name',
>                     .person 'location' as city,
>                     .favorite_books()[
>                         .title,
>                         .cost() as expense
>                         ] as books}
>             }
> There are more details here:

Personally, I find the explicit approach more intuitive, since
you immediately see what you are going to get:

show_table_of_contents = context['show_table_of_contents']
author = context['author']
d = {
    'show_table_of_contents': show_table_of_contents,
    'author': {
        'name': author.person['name'],
        'city': author.person['location'],
        'books': [{
            'title': item.title,
            'expense': item.cost(),
            for item in author.favorite_books()],

I particularly find this part non-intuitive:

>                     .favorite_books()[
>                         .title,
>                         .cost() as expense
>                         ] as books}

and would leave in the square brackets for __getitem__
lookups on these:

>                     .person 'name',
>                     .person 'location' as city,

For additional inspiration, you might want to look at XSLT
which provides similar transformations on XML data structures.

There are also a number of other transformation languages:

Regarding the term "DDL": that's normally used for "data definition
language" and doesn't really have all that much to do with
transforming data. You normally define data structures using
DDL - without actually putting data into those structures.

Why not "PyDTL".... Python data transformation language ?!

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Nov 18 2009)
>>> Python/Zope Consulting and Support ...
>>> mxODBC.Zope.Database.Adapter ...   
>>> mxODBC, mxDateTime, mxTextTools ...

::: Try our new mxODBC.Connect Python Database Interface for free ! :::: Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-list mailing list