updated pre-PEP: The create statement

Michael Spencer mahs at telcopartners.com
Mon Apr 10 23:33:41 EDT 2006


Steven Bethard wrote:
> Azolex wrote:
>> Steven Bethard wrote:
>>> and named, nested hierarchies like XML documents could be created
>>> like::
>>>
>>>     create ETobject html:
>>>         "This statement would generate an ElementTree object"
>>>
>>>         create ETobject head:
>>>             "generate the head"
>>>             ...
>>>
>>>         create ETobject body:
>>>             "generate the body"
>>>             ...
>> I think this is is a most important eventual use-case, and would like to 
>> see it better worked out - or else declared outside the scope of the 
>> proposed statement. As far as I can see, this does not cut it, since xml 
>> and html allow /sequencial repetition/ of tags and the results of the 
>> statement suites are passed as unordered namespaces/dicts.
> 
> Good point.  The code above would only work if you didn't care about the 
> order of elements.  I'm half inclined to pull the example, but the 
> original was due to Michele Simionato, and I haven't quite convinced 
> myself yet that you couldn't hack it to have ETObject append to an 
> internal list somehow to keep order.  Michele?  Did you have a plan for 
> how this would work?
> 
> STeVe
You've probably considered this, but I didn't see it in the PEP.  How about 
returning an dict with 'history' for the namespace, e.g. in essence:

class HistoryDict(dict):
     def __init__(self, *a, **kw):
         self.history = []
         super(HistoryDict, self).__init__(*a, **kw)
     def __setitem__(self, k, v):
         self.history.append((k, v))
         super(HistoryDict, self).__setitem__(k, v)

this would behave as a normal dict, but the additional history attribute could 
be useful in cases where order or duplicated names are significant, such as the 
xml example above, database table descriptions, overloaded function definitions...

Michael




More information about the Python-list mailing list