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', [])])
new empty dictionary
|
__init__(self,
*args,
**kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature |
source code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
None
|
|
|
a shallow copy of D
|
|
|
list of D's (key, value) pairs, as 2-tuples
|
|
|
an iterator over the (key, value) items of D
|
|
|
list of D's keys
|
|
|
an iterator over the keys of D
|
|
|
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
|
|
|
D.get(k,d), also set D[k]=d if k not in D
|
|
|
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
|
|
|
an iterator over the values of D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a shallow copy of D
|
|
|
an iterator over the keys of D
|
|
|
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__
|
|
New dict with keys from S and values equal to v
|
|
|
Inherited from object:
__class__
|
__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)
|
x==y
- Overrides:
dict.__eq__
- (inherited documentation)
|
x!=y
- Overrides:
dict.__ne__
- (inherited documentation)
|
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)
|
Remove all items from D.
- Returns: None
- Overrides:
dict.clear
- (inherited documentation)
|
- Returns: a shallow copy of D
- Overrides:
dict.copy
- (inherited documentation)
|
- Returns: list of D's (key, value) pairs, as 2-tuples
- Overrides:
dict.items
- (inherited documentation)
|
- Returns: an iterator over the (key, value) items of D
- Overrides:
dict.iteritems
- (inherited documentation)
|
- Returns: list of D's keys
- Overrides:
dict.keys
- (inherited documentation)
|
iter(x)
- Returns: an iterator over the keys of D
- Overrides:
dict.iterkeys
- (inherited documentation)
|
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)
|
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)
|
- Returns: D.get(k,d), also set D[k]=d if k not in D
- Overrides:
dict.setdefault
- (inherited documentation)
|
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)
|
- Returns: list of D's values
- Overrides:
dict.values
- (inherited documentation)
|
- Returns: an iterator over the values of D
- Overrides:
dict.itervalues
- (inherited documentation)
|
repr(x)
- Overrides:
object.__repr__
- (inherited documentation)
|
iter(x)
- Returns: an iterator over the keys of D
- Overrides:
dict.__iter__
- (inherited documentation)
|