Yet another ordered dictionary implementation

Tom Anderson twic at urchin.earth.li
Fri Nov 25 22:09:48 EST 2005


What up yalls,

Since i've been giving it all that all over the ordered dictionary thread 
lately, i thought i should put my fingers where my mouth is and write one 
myself:

http://urchin.earth.li/~twic/odict.py

It's nothing fancy, but it does what i think is right.

The big thing that i'm not happy with is the order list (what Larosa and 
Foord call 'sequence', i call 'order', just to be a pain); this is a list 
of keys, which for many purposes is ideal, but does mean that there are 
things you might want to do with the order that you can't do with normal 
python idioms. For example, say we wanted to move the last item in the 
order to be first; if this was a normal list, we'd say:

od.order.insert(0, od.order.pop())

But we can't do that here - the argument to the insert is just a key, so 
there isn't enough information to make an entry in the dict. To make up 
for this, i've added move and swap methods on the list, but this still 
isn't idiomatic.

In order to have idiomatic order manipulation, i think we need to make the 
order list a list of items - that is, (key, value) pairs. Then, there's 
enough information in the results of a pop to support an insert. This also 
allows us to implement the various other mutator methods on the order 
lists that i've had to rub out in my code.

However, this does seem somehow icky to me. I can't quite put my finger on 
it, but it seems to violate Once And Only Once. Also, even though the 
above idiom becomes possible, it leads to futile remove-reinsert cycles in 
the dict bit, which it would be nice to avoid.

Thoughts?

tom

-- 
I content myself with the Speculative part [...], I care not for the
Practick. I seldom bring any thing to use, 'tis not my way. Knowledge
is my ultimate end. -- Sir Nicholas Gimcrack



More information about the Python-list mailing list