我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用collections.ChainMap()。
def _initialize_preferences(cls): if id(cls) in cls._initialized: return cls._initialized.add(id(cls)) if cls == Quantity: prefs = DEFAULTS else: parent = cls.__mro__[1] # for some reason I cannot get super to work right prefs = parent._preferences # copy dict so any changes made to parent's preferences do not affect us prefs = dict(prefs) cls._preferences = ChainMap({}, prefs) # use chain to support use of contexts # put empty map in as first so user never accidentally deletes or # changes one of the initial preferences # set preferences {{{3
def chainmap_context_factory(parent_context=None): """ A ``ChainMap`` context, to avoid copying any data and yet preserve strict one-way inheritance (just like with dict copying) """ if parent_context is None: # initial context return ChainMap() else: # inherit context if not isinstance(parent_context, ChainMap): # if a dict context was previously used, then convert # (without modifying the original dict) parent_context = ChainMap(parent_context) return parent_context.new_child()
def count(start=0, step=1): """ ``itertools.count`` in Py 2.6 doesn't accept a step parameter. This is an enhanced version of ``itertools.count`` for Py2.6 equivalent to ``itertools.count`` in Python 2.7+. """ while True: yield start start += step ######################################################################## ### ChainMap (helper for configparser and string.Template) ### From the Py3.4 source code. See also: ### https://github.com/kkxue/Py2ChainMap/blob/master/py2chainmap.py ########################################################################
def __init__(self, *maps): '''Initialize a ChainMap by setting *maps* to the given mappings. If no mappings are provided, a single empty dictionary is used. ''' self.maps = list(maps) or [{}] # always at least one map
def fromkeys(cls, iterable, *args): 'Create a ChainMap with a single dict created from the iterable.' return cls(dict.fromkeys(iterable, *args))
def copy(self): 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]' return self.__class__(self.maps[0].copy(), *self.maps[1:])
def new_child(self): # like Django's Context.push() 'New ChainMap with a new dict followed by all previous maps.' return self.__class__({}, *self.maps)
def parents(self): # like Django's Context.pop() 'New ChainMap from maps[1:].' return self.__class__(*self.maps[1:])
def set_unit_system(unit_system): """Activates a unit system. The default unit system is 'mks'. Calling this function changes the active unit system to the one with the specified name. Only constants associated with the active unit system or not associated with a unit system are available for use. :arg str unit_system: Name of the desired unit system. A *KeyError* is raised if *unit_system* does not correspond to a known unit system. Example:: >>> from quantiphy import Quantity, set_unit_system >>> set_unit_system('cgs') >>> print(Quantity('h').render(show_label='f')) h = 6.6261e-27 erg-s -- Plank's constant >>> set_unit_system('mks') >>> print(Quantity('h').render(show_label='f')) h = 662.61e-36 J-s -- Plank's constant """ global _active_constants _active_constants = ChainMap( _constants[None], _constants[unit_system] )
def __aenter__(self): self.writes = dict() return ChainMap(self.writes, self)
def __enter__(self): self.writes = dict() return ChainMap(self.writes, self)
def resolved(self): """Provides a mapping that uses scope resolution for variables""" return ChainMap(*self.resolution_order_variables)