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: > > http://showellonprogramming.blogspot.com/2009/11/more-on-python-deep-copy-schema.html
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: http://en.wikipedia.org/wiki/Model_Transformation_Language http://en.wikipedia.org/wiki/Data_transformation 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 eGenix.com Professional Python Services directly from the Source (#1, Nov 18 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com 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 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list