Package dics :: Module opdict :: Class opdict
[hide private]
[frames] | no frames]

Class opdict
source code

object --+    
         |    
      dict --+
             |
            opdict

Minimal order preserving dictionary. Uses a list of keys in addition to an internal dictionary to keep track of the order in which entries are added to the dictionary. Calls to keys(), values(), items(), iterkeys(), itervalues() and iteritems() and popitem() will return data in the order that it was added to the dictionary.

This class is intended as a drop in replacement for the builtin dict type and replicates all its methods. While the internal representation would enable the addition of mutable sequence methods such as slices these have not (yet) been implemented.

The motivation for creating this class was to stop the standard library ConfigParser module randomising the order of sections and options when saving modified configs back to a file.

Anthony Horton horton@ast.cam.ac.uk 20050216

Instance Methods [hide private]
  __init__(self, arg={}, **kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
  __delitem__(self, key)
del x[y]
  __setitem__(self, key, value)
x[i]=y
  __iter__(self)
iter(x)
  __str__(self)
str(x)
  __repr__(self)
repr(x)
  keys(self)
  values(self)
  items(self)
  iterkeys(self)
  itervalues(self)
  iteritems(self)
  update(self, opd)
Update D from E: for k in E.keys(): D[k] = E[k]
  pop(self, key, default=None)
If key is not found, d is returned if given, otherwise KeyError is raised
  popitem(self)
2-tuple; but raise KeyError if D is empty
  copy(self)
  clear(self)
Remove all items from D.
  fromkeys(keys, value=None)
v defaults to None.
  setdefault(self, key, default=None)

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

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__


Class Variables [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, arg={}, **kwargs)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: dict.__init__
(inherited documentation)

__delitem__(self, key)
(Index deletion operator)

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

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

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

__iter__(self)

source code 
iter(x)
Overrides: dict.__iter__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 
str(x)
Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

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

keys(self)

source code 
Returns:
list of D's keys

Overrides: dict.keys
(inherited documentation)

values(self)

source code 
Returns:
list of D's values

Overrides: dict.values
(inherited documentation)

items(self)

source code 
Returns:
list of D's (key, value) pairs, as 2-tuples

Overrides: dict.items
(inherited documentation)

iterkeys(self)

source code 
Returns:
an iterator over the keys of D

Overrides: dict.iterkeys
(inherited documentation)

itervalues(self)

source code 
Returns:
an iterator over the values of D

Overrides: dict.itervalues
(inherited documentation)

iteritems(self)

source code 
Returns:
an iterator over the (key, value) items of D

Overrides: dict.iteritems
(inherited documentation)

update(self, opd)

source code 
Update D from E: for k in E.keys(): D[k] = E[k]
Returns:
None

Overrides: dict.update
(inherited documentation)

pop(self, key, default=None)

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)

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)

copy(self)

source code 
Returns:
a shallow copy of D

Overrides: dict.copy
(inherited documentation)

clear(self)

source code 
Remove all items from D.
Returns:
None

Overrides: dict.clear
(inherited documentation)

fromkeys(keys, value=None)

source code 
v defaults to None.
Returns:
New dict with keys from S and values equal to v

Overrides: dict.fromkeys
(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)