An ordered dictionary for the Python library?

Diez B. Roggisch deets at nospam.web.de
Sat Sep 15 03:32:17 EDT 2007


Mark Summerfield schrieb:
> On 14 Sep, 21:23, James Stroud <jstr... at mbi.ucla.edu> wrote:
>> Mark Summerfield wrote:
>>> I guess I'll have to rename my module (although unfortunately, my book
>>> has just gone into production and I have a (simpler) example of what I
>>> considered to be an ordered dict, so I will be adding to the
>>> terminology confusion). That notwithstanding, given that it is a
>>> sorteddict, how is the API?
>> I must think the API good because I have been implementing, in parallel
>> with this discussion, my own "OrderedDict" with a very similar API (this
>> is part of a larger project where I recently found the need to have a
>> well-implemented ordered dict). The only real omission I see is to allow
>> instantiating a "sorted dict" with an optional cmp function--to allow
>> the generalization of such features as case-independence, etc.
> 
> I tend to create different orderings by munging the keys rather than
> by having optional cmp functions (as you'll see in the sorteddict.py
> docstring). I've now put sorteddict on PyPI (with docs & tests):
> http://pypi.python.org/pypi/sorteddict

The advantage of a passed cmp-function is that the context of usage 
hasn't to be aware of any change in semantics. Yet for better 
performance, the cmp-function might be a tad to slow, better use a 
key-function like for sorted/sort.

Additionally, I'd change the parameter names of the keys()-method to 
start/end and make more clear if end is inclusive or not. The 
first/secondindex-names are pretty horrible IMHO, because the 
essentially enumerate parameters. Who wants to work with a method

foo(arg1, arg2, arg3)

? :)

Diez



More information about the Python-list mailing list