Package mapproxy :: Package core :: Module odict :: Class odict
[hide private]
[frames] | no frames]

Class odict

source code

object --+    
         |    
      dict --+
             |
            odict

Ordered dict example implementation.

This is the proposed interface for a an ordered dict as proposed on the Python mailinglist (proposal).

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 mappings:

>>> d = odict([('a', 'b'), ('c', 'd')])
>>> d.update({'foo': 'bar'})
>>> d
odict.odict([('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 odict 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)
odict.odict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
>>> d.copy()
odict.odict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
>>> odict(d)
odict.odict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
>>> d['spam'] = []
>>> d2 = deepcopy(d)
>>> d2['spam'].append('eggs')
>>> d
odict.odict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])
>>> d2
odict.odict([('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 odict as well:

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

And sort it like a list:

>>> d.sort(key=lambda x: x[0].lower())
>>> d
odict.odict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])
Instance Methods [hide private]
new empty dictionary

__init__(self, *args, **kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__delitem__(self, key)
del x[y]
source code
 
__setitem__(self, key, item)
x[i]=y
source code
 
__deepcopy__(self, memo=None) source code
 
__getstate__(self) source code
 
__setstate__(self, d) source code
 
__reversed__(self) source code
 
__eq__(self, other)
x==y
source code
 
__ne__(self, other)
x!=y
source code
 
__cmp__(self, other)
cmp(x,y)
source code
None
clear(self)
Remove all items from D.
source code
a shallow copy of D
copy(self) source code
list of D's (key, value) pairs, as 2-tuples
items(self) source code
an iterator over the (key, value) items of D
iteritems(self) source code
list of D's keys
keys(self) source code
an iterator over the keys of D
iterkeys(self)
iter(x)
source code
v, remove specified key and return the corresponding value
pop(self, key, default=object())
If key is not found, d is returned if given, otherwise KeyError is raised
source code
(k, v), remove and return some (key, value) pair as a
popitem(self, key)
2-tuple; but raise KeyError if D is empty
source code
D.get(k,d), also set D[k]=d if k not in D
setdefault(self, key, default=None) source code
None
update(self, *args, **kwargs)
Update D from E and F: for k in E: D[k] = E[k] (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]
source code
list of D's values
values(self) source code
an iterator over the values of D
itervalues(self) source code
 
index(self, item) source code
 
byindex(self, item) source code
 
reverse(self) source code
 
sort(self, *args, **kwargs) source code
 
__repr__(self)
repr(x)
source code
a shallow copy of D
__copy__(self) source code
an iterator over the keys of D
__iter__(self)
iter(x)
source code

Inherited from dict: __contains__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __le__, __len__, __lt__, __new__, get, has_key

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Methods [hide private]
New dict with keys from S and values equal to v
fromkeys(cls, iterable, default=None)
v defaults to None.
source code
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *args, **kwargs)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

__delitem__(self, key)
(Index deletion operator)

source code 
del x[y]
Overrides: dict.__delitem__
(inherited documentation)

__setitem__(self, key, item)
(Index assignment operator)

source code 
x[i]=y
Overrides: dict.__setitem__
(inherited documentation)

__eq__(self, other)
(Equality operator)

source code 
x==y
Overrides: dict.__eq__
(inherited documentation)

__ne__(self, other)

source code 
x!=y
Overrides: dict.__ne__
(inherited documentation)

__cmp__(self, other)
(Comparison operator)

source code 
cmp(x,y)
Overrides: dict.__cmp__
(inherited documentation)

fromkeys(cls, iterable, default=None)
Class Method

source code 
v defaults to None.
Returns: New dict with keys from S and values equal to v
Overrides: dict.fromkeys
(inherited documentation)

clear(self)

source code 
Remove all items from D.
Returns: None
Overrides: dict.clear
(inherited documentation)

copy(self)

source code 
Returns: a shallow copy of D
Overrides: dict.copy
(inherited documentation)

items(self)

source code 
Returns: list of D's (key, value) pairs, as 2-tuples
Overrides: dict.items
(inherited documentation)

iteritems(self)

source code 
Returns: an iterator over the (key, value) items of D
Overrides: dict.iteritems
(inherited documentation)

keys(self)

source code 
Returns: list of D's keys
Overrides: dict.keys
(inherited documentation)

iterkeys(self)

source code 
iter(x)
Returns: an iterator over the keys of D
Overrides: dict.iterkeys
(inherited documentation)

pop(self, key, default=object())

source code 
If key is not found, d is returned if given, otherwise KeyError is raised
Returns: v, remove specified key and return the corresponding value
Overrides: dict.pop
(inherited documentation)

popitem(self, key)

source code 
2-tuple; but raise KeyError if D is empty
Returns: (k, v), remove and return some (key, value) pair as a
Overrides: dict.popitem
(inherited documentation)

setdefault(self, key, default=None)

source code 
Returns: D.get(k,d), also set D[k]=d if k not in D
Overrides: dict.setdefault
(inherited documentation)

update(self, *args, **kwargs)

source code 
Update D from E and F: for k in E: D[k] = E[k] (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]
Returns: None
Overrides: dict.update
(inherited documentation)

values(self)

source code 
Returns: list of D's values
Overrides: dict.values
(inherited documentation)

itervalues(self)

source code 
Returns: an iterator over the values of D
Overrides: dict.itervalues
(inherited documentation)

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: object.__repr__
(inherited documentation)

__iter__(self)

source code 
iter(x)
Returns: an iterator over the keys of D
Overrides: dict.__iter__
(inherited documentation)