Mailing List Archive

OrderedDict.py
Is there a need for a module which allows for ordered processing of an
dictionary?

I have one written, and it works fine I think. Basically, it allows for a
dictionary to be treated like a list, but have the data structure of
dictionary. Key-value pairs retain the order you entered them in. Unless
you specifically change them. I'm currently working on more methods, to
allow it to be sliced like an array.

currently, it wouldn't allow for numeric keys, because the __getitem__
finds out what you sent, an integer (numeric index) or a string (a
dictionary key). the __getslice__ and __setslice__ aren't a problem. I
could make a special class for indices for getting dict[4]...

something like:

dict[Index(4)] instead of dict[4]

but I'd like to be as seamless as possible.

I'll be putting the code on my web site soon.

--
Jeff Pinyan (jeffp@crusoe.net)
www.crusoe.net/~jeffp

Crusoe Communications, Inc.
732-728-9800
www.crusoe.net
OrderedDict.py [ In reply to ]
evil Japh (or possibly his good twin Jeff Pinyan) wrote:

> Is there a need for a module which allows for ordered processing of an
> dictionary?

I think there is a need for this. There is a bit of code I wrote for
PythonWin where some values are displayed in a UI and then read back in
after user modification. It did not matter what order the dictionary was
displayed in so long as it could be read back in in the same order to
match up. Nothing in the documentation specifies that asking for the
elements (or keys) of a dictionary returns elements (or keys) in the
same order each time, even if no modifications are done to the
dictionary between accesses. Therefore, each time the dictionary was
used, there was a sequence like:

sortedkeys = formats.keys()
sortedkeys.sort()
for f in sortedkeys:

Your module may have made this code easier and also clearer.

Neil Hodgson
Fujitsu Australia Software Technology
OrderedDict.py [ In reply to ]
evil Japh wrote:
>
> Is there a need for a module which allows for ordered processing of an
> dictionary?
>
> I have one written, and it works fine I think. Basically, it allows for a
> dictionary to be treated like a list, but have the data structure of
> dictionary. Key-value pairs retain the order you entered them in. Unless
> you specifically change them. I'm currently working on more methods, to
> allow it to be sliced like an array.

Your approach sounds like a sequence of (key, value) tuples. It can't really be
used as a general-purpose dictionary, because searching for an arbitrary element
using the key is going to be linear in the number of elements, whereas the
native Python dictionary is (normal-case) constant-time for the same lookup.

Perhaps you are maintaining the ordered keys in a separate sequence on the
side? This construct is more useful, but again has scalability problems:
removing items from the list is harder, and more expensive, and you store the
keys twice (if keys are objects, this is only storing refernces twice).


--
=========================================================
Tres Seaver tseaver@palladion.com 713-523-6582
Palladion Software http://www.palladion.com