Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes | Private Attributes | Static Private Attributes
collections.OrderedDict Class Reference
Inheritance diagram for collections.OrderedDict:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def __setitem__
def __delitem__
def __iter__
def __reversed__
def clear
def popitem
def move_to_end
def __sizeof__
def pop
def setdefault
def __repr__
def __reduce__
def copy
def fromkeys
def __eq__

Static Public Attributes

 update = __updateMutableMapping.update
 keys = MutableMapping.keys
 values = MutableMapping.values
 items = MutableMapping.items

Private Attributes

 __hardroot
 __root
 __map

Static Private Attributes

 __ne__ = MutableMapping.__ne__
tuple __marker = object()

Detailed Description

Definition at line 25 of file collections.py.


Constructor & Destructor Documentation

def collections.OrderedDict.__init__ (   self,
  args,
  kwds 
)
Initialize an ordered dictionary.  The signature is the same as
regular dictionaries, but keyword arguments are not recommended because
their insertion order is arbitrary.

Definition at line 40 of file collections.py.

00040 
00041     def __init__(self, *args, **kwds):
00042         '''Initialize an ordered dictionary.  The signature is the same as
00043         regular dictionaries, but keyword arguments are not recommended because
00044         their insertion order is arbitrary.
00045 
00046         '''
00047         if len(args) > 1:
00048             raise TypeError('expected at most 1 arguments, got %d' % len(args))
00049         try:
00050             self.__root
00051         except AttributeError:
00052             self.__hardroot = _Link()
00053             self.__root = root = _proxy(self.__hardroot)
00054             root.prev = root.next = root
00055             self.__map = {}
00056         self.__update(*args, **kwds)

Here is the caller graph for this function:


Member Function Documentation

def collections.OrderedDict.__delitem__ (   self,
  key,
  dict_delitem = dict.__delitem__ 
)

Definition at line 71 of file collections.py.

00071 
00072     def __delitem__(self, key, dict_delitem=dict.__delitem__):
00073         'od.__delitem__(y) <==> del od[y]'
00074         # Deleting an existing item uses self.__map to find the link which gets
00075         # removed by updating the links in the predecessor and successor nodes.
00076         dict_delitem(self, key)
00077         link = self.__map.pop(key)
00078         link_prev = link.prev
00079         link_next = link.next
00080         link_prev.next = link_next
00081         link_next.prev = link_prev

def collections.OrderedDict.__eq__ (   self,
  other 
)
od.__eq__(y) <==> od==y.  Comparison to another OD is order-sensitive
while comparison to a regular mapping is order-insensitive.

Definition at line 224 of file collections.py.

00224 
00225     def __eq__(self, other):
00226         '''od.__eq__(y) <==> od==y.  Comparison to another OD is order-sensitive
00227         while comparison to a regular mapping is order-insensitive.
00228 
00229         '''
00230         if isinstance(other, OrderedDict):
00231             return len(self)==len(other) and \
00232                    all(p==q for p, q in zip(self.items(), other.items()))
00233         return dict.__eq__(self, other)
00234 

Here is the caller graph for this function:

Definition at line 82 of file collections.py.

00082 
00083     def __iter__(self):
00084         'od.__iter__() <==> iter(od)'
00085         # Traverse the linked list in order.
00086         root = self.__root
00087         curr = root.next
00088         while curr is not root:
00089             yield curr.key
00090             curr = curr.next

Definition at line 199 of file collections.py.

00199 
00200     def __reduce__(self):
00201         'Return state information for pickling'
00202         items = [[k, self[k]] for k in self]
00203         inst_dict = vars(self).copy()
00204         for k in vars(OrderedDict()):
00205             inst_dict.pop(k, None)
00206         if inst_dict:
00207             return (self.__class__, (items,), inst_dict)
00208         return self.__class__, (items,)

Definition at line 193 of file collections.py.

00193 
00194     def __repr__(self):
00195         'od.__repr__() <==> repr(od)'
00196         if not self:
00197             return '%s()' % (self.__class__.__name__,)
00198         return '%s(%r)' % (self.__class__.__name__, list(self.items()))

Definition at line 91 of file collections.py.

00091 
00092     def __reversed__(self):
00093         'od.__reversed__() <==> reversed(od)'
00094         # Traverse the linked list in reverse order.
00095         root = self.__root
00096         curr = root.prev
00097         while curr is not root:
00098             yield curr.key
00099             curr = curr.prev

def collections.OrderedDict.__setitem__ (   self,
  key,
  value,
  dict_setitem = dict.__setitem__,
  proxy = _proxy,
  Link = _Link 
)

Definition at line 58 of file collections.py.

00058 
00059                     dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
00060         'od.__setitem__(i, y) <==> od[i]=y'
00061         # Setting a new item creates a new link at the end of the linked list,
00062         # and the inherited dictionary is updated with the new key/value pair.
00063         if key not in self:
00064             self.__map[key] = link = Link()
00065             root = self.__root
00066             last = root.prev
00067             link.prev, link.next, link.key = last, root, key
00068             last.next = link
00069             root.prev = proxy(link)
00070         dict_setitem(self, key, value)

Definition at line 154 of file collections.py.

00154 
00155     def __sizeof__(self):
00156         sizeof = _sys.getsizeof
00157         n = len(self) + 1                       # number of links including root
00158         size = sizeof(self.__dict__)            # instance dictionary
00159         size += sizeof(self.__map) * 2          # internal dict and inherited dict
00160         size += sizeof(self.__hardroot) * n     # link objects
00161         size += sizeof(self.__root) * n         # proxy objects
00162         return size

Definition at line 100 of file collections.py.

00100 
00101     def clear(self):
00102         'od.clear() -> None.  Remove all items from od.'
00103         root = self.__root
00104         root.prev = root.next = root
00105         self.__map.clear()
00106         dict.clear(self)

Here is the caller graph for this function:

Definition at line 209 of file collections.py.

00209 
00210     def copy(self):
00211         'od.copy() -> a shallow copy of od'
00212         return self.__class__(self)

def collections.OrderedDict.fromkeys (   cls,
  iterable,
  value = None 
)
OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
If not specified, the value defaults to None.

Definition at line 214 of file collections.py.

00214 
00215     def fromkeys(cls, iterable, value=None):
00216         '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
00217         If not specified, the value defaults to None.
00218 
00219         '''
00220         self = cls()
00221         for key in iterable:
00222             self[key] = value
00223         return self

def collections.OrderedDict.move_to_end (   self,
  key,
  last = True 
)
Move an existing element to the end (or beginning if last==False).

Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).

Definition at line 130 of file collections.py.

00130 
00131     def move_to_end(self, key, last=True):
00132         '''Move an existing element to the end (or beginning if last==False).
00133 
00134         Raises KeyError if the element does not exist.
00135         When last=True, acts like a fast version of self[key]=self.pop(key).
00136 
00137         '''
00138         link = self.__map[key]
00139         link_prev = link.prev
00140         link_next = link.next
00141         link_prev.next = link_next
00142         link_next.prev = link_prev
00143         root = self.__root
00144         if last:
00145             last = root.prev
00146             link.prev = last
00147             link.next = root
00148             last.next = root.prev = link
00149         else:
00150             first = root.next
00151             link.prev = root
00152             link.next = first
00153             root.next = first.prev = link

def collections.OrderedDict.pop (   self,
  key,
  default = __marker 
)
od.pop(k[,d]) -> v, remove specified key and return the corresponding
value.  If key is not found, d is returned if given, otherwise KeyError
is raised.

Definition at line 171 of file collections.py.

00171 
00172     def pop(self, key, default=__marker):
00173         '''od.pop(k[,d]) -> v, remove specified key and return the corresponding
00174         value.  If key is not found, d is returned if given, otherwise KeyError
00175         is raised.
00176 
00177         '''
00178         if key in self:
00179             result = self[key]
00180             del self[key]
00181             return result
00182         if default is self.__marker:
00183             raise KeyError(key)
00184         return default

Here is the caller graph for this function:

def collections.OrderedDict.popitem (   self,
  last = True 
)
od.popitem() -> (k, v), return and remove a (key, value) pair.
Pairs are returned in LIFO order if last is true or FIFO order if false.

Definition at line 107 of file collections.py.

00107 
00108     def popitem(self, last=True):
00109         '''od.popitem() -> (k, v), return and remove a (key, value) pair.
00110         Pairs are returned in LIFO order if last is true or FIFO order if false.
00111 
00112         '''
00113         if not self:
00114             raise KeyError('dictionary is empty')
00115         root = self.__root
00116         if last:
00117             link = root.prev
00118             link_prev = link.prev
00119             link_prev.next = root
00120             root.prev = link_prev
00121         else:
00122             link = root.next
00123             link_next = link.next
00124             root.next = link_next
00125             link_next.prev = root
00126         key = link.key
00127         del self.__map[key]
00128         value = dict.pop(self, key)
00129         return key, value

def collections.OrderedDict.setdefault (   self,
  key,
  default = None 
)

Definition at line 185 of file collections.py.

00185 
00186     def setdefault(self, key, default=None):
00187         'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
00188         if key in self:
00189             return self[key]
00190         self[key] = default
00191         return default


Member Data Documentation

Definition at line 51 of file collections.py.

Definition at line 54 of file collections.py.

tuple collections.OrderedDict.__marker = object() [static, private]

Definition at line 169 of file collections.py.

Definition at line 167 of file collections.py.

Definition at line 52 of file collections.py.

Definition at line 166 of file collections.py.

Definition at line 164 of file collections.py.

collections.OrderedDict.update = __updateMutableMapping.update [static]

Definition at line 163 of file collections.py.

Definition at line 165 of file collections.py.


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