Back to index

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

List of all members.

Public Member Functions

def __init__
def __missing__
def __getitem__
def get
def __len__
def __iter__
def __contains__
def __bool__
def __repr__
def fromkeys
def copy
def new_child
def parents
def __setitem__
def __delitem__
def popitem
def pop
def clear
def update
def setdefault
def keys
def items
def values
def __eq__
def __ne__
def __subclasshook__
def __subclasshook__
def __subclasshook__
def __new__
def __new__
def __new__
def register
def register
def register
def __instancecheck__
def __instancecheck__
def __instancecheck__
def __subclasscheck__
def __subclasscheck__
def __subclasscheck__

Public Attributes

 maps

Static Private Attributes

 __copy__ = copy

Detailed Description

A ChainMap groups multiple dicts (or other mappings) together
to create a single, updateable view.

The underlying mappings are stored in a list.  That list is public and can
accessed or updated using the *maps* attribute.  There is no other state.

Lookups search the underlying mappings successively until a key is found.
In contrast, writes, updates, and deletions only operate on the first
mapping.

Definition at line 678 of file collections.py.


Constructor & Destructor Documentation

def collections._ChainMap.__init__ (   self,
  maps 
)
Initialize a ChainMap by setting *maps* to the given mappings.
If no mappings are provided, a single empty dictionary is used.

Definition at line 691 of file collections.py.

00691 
00692     def __init__(self, *maps):
00693         '''Initialize a ChainMap by setting *maps* to the given mappings.
00694         If no mappings are provided, a single empty dictionary is used.
00695 
00696         '''
00697         self.maps = list(maps) or [{}]          # always at least one map

Here is the caller graph for this function:


Member Function Documentation

Definition at line 721 of file collections.py.

00721 
00722     def __bool__(self):
00723         return any(self.maps)

Here is the call graph for this function:

def collections._ChainMap.__contains__ (   self,
  key 
)

Reimplemented from _abcoll.Mapping.

Definition at line 718 of file collections.py.

00718 
00719     def __contains__(self, key):
00720         return any(key in m for m in self.maps)

Here is the call graph for this function:

def collections._ChainMap.__delitem__ (   self,
  key 
)

Reimplemented from _abcoll.MutableMapping.

Definition at line 752 of file collections.py.

00752 
00753     def __delitem__(self, key):
00754         try:
00755             del self.maps[0][key]
00756         except KeyError:
00757             raise KeyError('Key not found in the first mapping: {!r}'.format(key))

def _abcoll.Mapping.__eq__ (   self,
  other 
) [inherited]

Definition at line 380 of file _abcoll.py.

00380 
00381     def __eq__(self, other):
00382         if not isinstance(other, Mapping):
00383             return NotImplemented
00384         return dict(self.items()) == dict(other.items())

Here is the call graph for this function:

Here is the caller graph for this function:

def collections._ChainMap.__getitem__ (   self,
  key 
)

Reimplemented from _abcoll.Mapping.

Definition at line 701 of file collections.py.

00701 
00702     def __getitem__(self, key):
00703         for mapping in self.maps:
00704             try:
00705                 return mapping[key]             # can't use 'key in mapping' with defaultdict
00706             except KeyError:
00707                 pass
00708         return self.__missing__(key)            # support subclasses that define __missing__

Here is the call graph for this function:

Here is the caller graph for this function:

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

Reimplemented from _abcoll.Iterable.

Definition at line 715 of file collections.py.

00715 
00716     def __iter__(self):
00717         return iter(set().union(*self.maps))

Here is the call graph for this function:

Reimplemented from _abcoll.Sized.

Definition at line 712 of file collections.py.

00712 
00713     def __len__(self):
00714         return len(set().union(*self.maps))     # reuses stored hash values if possible

Here is the call graph for this function:

def collections._ChainMap.__missing__ (   self,
  key 
)

Definition at line 698 of file collections.py.

00698 
00699     def __missing__(self, key):
00700         raise KeyError(key)

Here is the caller graph for this function:

def _abcoll.Mapping.__ne__ (   self,
  other 
) [inherited]

Definition at line 385 of file _abcoll.py.

00385 
00386     def __ne__(self, other):
00387         return not (self == other)
00388 

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

Definition at line 725 of file collections.py.

00725 
00726     def __repr__(self):
00727         return '{0.__class__.__name__}({1})'.format(
00728             self, ', '.join(map(repr, self.maps)))

def collections._ChainMap.__setitem__ (   self,
  key,
  value 
)

Reimplemented from _abcoll.MutableMapping.

Definition at line 749 of file collections.py.

00749 
00750     def __setitem__(self, key, value):
00751         self.maps[0][key] = value

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def _abcoll.Iterable.__subclasshook__ (   cls,
  C 
) [inherited]

Reimplemented in _abcoll.Iterator.

Definition at line 74 of file _abcoll.py.

00074 
00075     def __subclasshook__(cls, C):
00076         if cls is Iterable:
00077             if any("__iter__" in B.__dict__ for B in C.__mro__):
00078                 return True
00079         return NotImplemented
00080 

Here is the call graph for this function:

def _abcoll.Sized.__subclasshook__ (   cls,
  C 
) [inherited]

Definition at line 119 of file _abcoll.py.

00119 
00120     def __subclasshook__(cls, C):
00121         if cls is Sized:
00122             if any("__len__" in B.__dict__ for B in C.__mro__):
00123                 return True
00124         return NotImplemented
00125 

Here is the call graph for this function:

def _abcoll.Container.__subclasshook__ (   cls,
  C 
) [inherited]

Definition at line 133 of file _abcoll.py.

00133 
00134     def __subclasshook__(cls, C):
00135         if cls is Container:
00136             if any("__contains__" in B.__dict__ for B in C.__mro__):
00137                 return True
00138         return NotImplemented
00139 

Here is the call graph for this function:

Reimplemented from _abcoll.MutableMapping.

Definition at line 772 of file collections.py.

00772 
00773     def clear(self):
00774         'Clear maps[0], leaving maps[1:] intact.'
00775         self.maps[0].clear()
00776 

Here is the caller graph for this function:

Definition at line 734 of file collections.py.

00734 
00735     def copy(self):
00736         'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
00737         return self.__class__(self.maps[0].copy(), *self.maps[1:])

def collections._ChainMap.fromkeys (   cls,
  iterable,
  args 
)

Definition at line 730 of file collections.py.

00730 
00731     def fromkeys(cls, iterable, *args):
00732         'Create a ChainMap with a single dict created from the iterable.'
00733         return cls(dict.fromkeys(iterable, *args))

def collections._ChainMap.get (   self,
  key,
  default = None 
)

Reimplemented from _abcoll.Mapping.

Definition at line 709 of file collections.py.

00709 
00710     def get(self, key, default=None):
00711         return self[key] if key in self else default

Here is the caller graph for this function:

def _abcoll.Mapping.items (   self) [inherited]

Reimplemented in weakref.WeakKeyDictionary, dbm.dumb._Database, weakref.WeakValueDictionary, and test.test_cfgparser.SortedDict.

Definition at line 374 of file _abcoll.py.

00374 
00375     def items(self):
00376         return ItemsView(self)

Here is the caller graph for this function:

def _abcoll.Mapping.keys (   self) [inherited]

Reimplemented in test.support.EnvironmentVarGuard, weakref.WeakKeyDictionary, dbm.dumb._Database, weakref.WeakValueDictionary, and test.test_cfgparser.SortedDict.

Definition at line 371 of file _abcoll.py.

00371 
00372     def keys(self):
00373         return KeysView(self)

Here is the caller graph for this function:

Definition at line 740 of file collections.py.

00740 
00741     def new_child(self):                        # like Django's Context.push()
00742         'New ChainMap with a new dict followed by all previous maps.'
00743         return self.__class__({}, *self.maps)

Definition at line 745 of file collections.py.

00745 
00746     def parents(self):                          # like Django's Context.pop()
00747         'New ChainMap from maps[1:].'
00748         return self.__class__(*self.maps[1:])

def collections._ChainMap.pop (   self,
  key,
  args 
)

Reimplemented from _abcoll.MutableMapping.

Definition at line 765 of file collections.py.

00765 
00766     def pop(self, key, *args):
00767         'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].'
00768         try:
00769             return self.maps[0].pop(key, *args)
00770         except KeyError:
00771             raise KeyError('Key not found in the first mapping: {!r}'.format(key))

Here is the caller graph for this function:

Reimplemented from _abcoll.MutableMapping.

Definition at line 758 of file collections.py.

00758 
00759     def popitem(self):
00760         'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.'
00761         try:
00762             return self.maps[0].popitem()
00763         except KeyError:
00764             raise KeyError('No keys found in the first mapping.')

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:

def _abcoll.MutableMapping.setdefault (   self,
  key,
  default = None 
) [inherited]

Reimplemented in os._Environ, weakref.WeakKeyDictionary, and weakref.WeakValueDictionary.

Definition at line 514 of file _abcoll.py.

00514 
00515     def setdefault(self, key, default=None):
00516         try:
00517             return self[key]
00518         except KeyError:
00519             self[key] = default
00520         return default
00521 
00522 MutableMapping.register(dict)
00523 

def _abcoll.MutableMapping.update (   args,
  kwds 
) [inherited]

Definition at line 493 of file _abcoll.py.

00493 
00494     def update(*args, **kwds):
00495         if len(args) > 2:
00496             raise TypeError("update() takes at most 2 positional "
00497                             "arguments ({} given)".format(len(args)))
00498         elif not args:
00499             raise TypeError("update() takes at least 1 argument (0 given)")
00500         self = args[0]
00501         other = args[1] if len(args) >= 2 else ()
00502 
00503         if isinstance(other, Mapping):
00504             for key in other:
00505                 self[key] = other[key]
00506         elif hasattr(other, "keys"):
00507             for key in other.keys():
00508                 self[key] = other[key]
00509         else:
00510             for key, value in other:
00511                 self[key] = value
00512         for key, value in kwds.items():
00513             self[key] = value

Here is the caller graph for this function:

def _abcoll.Mapping.values (   self) [inherited]

Reimplemented in weakref.WeakKeyDictionary, weakref.WeakValueDictionary, and test.test_cfgparser.SortedDict.

Definition at line 377 of file _abcoll.py.

00377 
00378     def values(self):
00379         return ValuesView(self)

Here is the caller graph for this function:


Member Data Documentation

collections._ChainMap.__copy__ = copy [static, private]

Definition at line 738 of file collections.py.

Definition at line 696 of file collections.py.


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