Logo Search packages:      
Sourcecode: zine version File versions  Download package

zine::utils::datastructures::OrderedDict Class Reference

List of all members.

Detailed Description

Simple ordered dict implementation.

It's a dict subclass and provides some list functions.  The implementation
of this class is inspired by the implementation of Babel but incorporates
some ideas from the `ordereddict`_ and Django's ordered dict.

The constructor and `update()` both accept iterables of tuples as well as

>>> d = OrderedDict([('a', 'b'), ('c', 'd')])
>>> d.update({'foo': 'bar'})
>>> d
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])

Keep in mind that when updating from dict-literals the order is not
preserved as these dicts are unsorted!

You can copy an OrderedDict like a dict by using the constructor, `copy.copy`
or the `copy` method and make deep copies with `copy.deepcopy`:

>>> from copy import copy, deepcopy
>>> copy(d)
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
>>> d.copy()
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
>>> OrderedDict(d)
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
>>> d['spam'] = []
>>> d2 = deepcopy(d)
>>> d2['spam'].append('eggs')
>>> d
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])
>>> d2
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', ['eggs'])])

All iteration methods as well as `keys`, `values` and `items` return
the values ordered by the the time the key-value pair is inserted:

>>> d.keys()
['a', 'c', 'foo', 'spam']
>>> d.values()
['b', 'd', 'bar', []]
>>> d.items()
[('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])]
>>> list(d.iterkeys())
['a', 'c', 'foo', 'spam']
>>> list(d.itervalues())
['b', 'd', 'bar', []]
>>> list(d.iteritems())
[('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])]

Index based lookup is supported too by `byindex` which returns the
key/value pair for an index:

>>> d.byindex(2)
('foo', 'bar')

You can reverse the OrderedDict as well:

>>> d.reverse()
>>> d
OrderedDict([('spam', []), ('foo', 'bar'), ('c', 'd'), ('a', 'b')])

And sort it like a list:

>>> d.sort(key=lambda x: x[0].lower())
>>> d
OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])

.. _proposal: http://thread.gmane.org/gmane.comp.python.devel/95316
.. _ordereddict: http://www.xs4all.nl/~anthon/Python/ordereddict/

Definition at line 97 of file datastructures.py.

Public Member Functions

def __cmp__
def __deepcopy__
def __delitem__
def __eq__
def __getstate__
def __init__
def __ne__
def __repr__
def __reversed__
def __setitem__
def __setstate__
def byindex
def clear
def copy
def fromkeys
def index
def items
def iteritems
def iterkeys
def itervalues
def keys
def pop
def popitem
def reverse
def setdefault
def sort
def update
def values

Private Attributes


Static Private Attributes

 __copy__ = copy
 __iter__ = iterkeys
tuple _keys = _PickleProtocol2Sucks()

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index