On Mon, 21 Nov 2005 01:27:22 +0100, Christoph Zwerschke <[EMAIL PROTECTED]> 
wrote:

>Fredrik Lundh wrote:
>> if you restructure the list somewhat
>>     d = (
>>         ('pid', ('Employee ID', 'int')),
>>         ('name', ('Employee name', 'varchar')),
>>         ('sal', ('Salary', 'float'))
>>         )
>> you can still loop over the list
>> ...
>> but you can easily generate an index when you need it:
>>     index = dict(d)
>
>That's exactly the kind of things I find myself doing too often and what 
>I was talking about: You are using *two* pretty redundant data 
>structures, a dictionary and a list/tuple to describe the same thing. 
>Ok, you can use a trick to automatically create the dictionary from the 
>tuple, but still it feels somewhat "unnatural" for me. A "ordered 
>dictionary" would be the more "natural" data structure here.
>
But, as has been mentioned**n, this is only one example of an ordering one
could make default for an "ordered" dictionary. Suppose you say it should
be ordered by insertion order, so
  d = OrderedDict(); d[1]='one'; d[2]='two' =>> list(d) => [1, 2]
ok, now we do d[1]='ein' and what is the order? list(d) => [2, 1] ??
Or do replacements not count as "insertions"? The devil is always going
to be in the details. Maybe you want a model that works more like a list
of key:value pairs with just optimized access to a pair by key name as
well as position in the list. Or maybe you want to permit append and
NOT prevent [('a',1), ('a':2)] and maybe d['a'] => [1, 2] ???

The point is that Python is a nice lego set, and pre-molded castles
don't re-use well, even if they suit a particular you to a t ;-)

Note that is isn't hard to snap a few pieces together to make an ordered
dict to your own specs. But IMO it belongs in pyPI or such, not in the system
library. At least until it gets a lot of mileage -- and MMV ;-)

>I also wanted to mention the uglyness in the definition (nested tuples), 
>but then I understood that even an ordered dictionary would not 
>eliminate that uglyness, since the curly braces are part of the Python 
>syntax and cannot be used for creating ordered dictionaries anyway. I 
>would have to define the ordered dictionary in the very same ugly way:
>
>d = odict(('pid', ('Employee ID', 'int')),
>       ('name', ('Employee name', 'varchar')),
>       ('sal', ('Salary', 'float')))
>
>(Unless the Python syntax would be extend to use double curly braces or 
>something for ordered dictionaries - but I understand that this is not 
>an option.)
>
Whatever your odict does, if I had type a lot of definitions for it
I think I would write a QnD helper to make this work:

 d = odict(prep("""

     pid,  Employee ID,   int
     name, Employee name, varchar # (comments to be ignored)
     sal, Salary, float           # alignment as above not mandatory
     other, Something else, long, additional elements, allowed in second tuple?
     """))

( posting delayed >12 hrs due to news server prob ;-/ )

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to