PyPedal::odict::OrderedDict Class Reference

Inheritance diagram for PyPedal::odict::OrderedDict:

Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__

Public Attributes

 strict


Detailed Description

A class of dictionary that keeps the insertion order of keys.

All appropriate methods return keys, items, or values in an ordered way.

All normal dictionary methods are available. Update and comparison is
restricted to other OrderedDict objects.

Various sequence methods are available, including the ability to explicitly
mutate the key ordering.

__contains__ tests:

>>> d = OrderedDict(((1, 3),))
>>> 1 in d
1
>>> 4 in d
0

__getitem__ tests:

>>> OrderedDict(((1, 3), (3, 2), (2, 1)))[2]
1
>>> OrderedDict(((1, 3), (3, 2), (2, 1)))[4]
Traceback (most recent call last):
KeyError: 4

__len__ tests:

>>> len(OrderedDict())
0
>>> len(OrderedDict(((1, 3), (3, 2), (2, 1))))
3

get tests:

>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.get(1)
3
>>> d.get(4) is None
1
>>> d.get(4, 5)
5
>>> d
OrderedDict([(1, 3), (3, 2), (2, 1)])

has_key tests:

>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d.has_key(1)
1
>>> d.has_key(4)
0

Definition at line 39 of file odict.py.


Member Function Documentation

def PyPedal::odict::OrderedDict::__init__ (   self,
  init_val = (),
  strict = False 
)

Create a new ordered dictionary. Cannot init from a normal dict,
nor from kwargs, since items order is undefined in those cases.

If the ``strict`` keyword argument is ``True`` (``False`` is the
default) then when doing slice assignment - the ``OrderedDict`` you are
assigning from *must not* contain any keys in the remaining dict.

>>> OrderedDict()
OrderedDict([])
>>> OrderedDict({1: 1})
Traceback (most recent call last):
TypeError: undefined order, cannot get items from dict
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d
OrderedDict([(1, 3), (3, 2), (2, 1)])
>>> OrderedDict(d)
OrderedDict([(1, 3), (3, 2), (2, 1)])

Reimplemented in PyPedal::odict::SequenceOrderedDict.

Definition at line 95 of file odict.py.


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

Generated on Thu May 1 12:33:43 2008 for PyPedal by  doxygen 1.5.5