Python collections.ChainMap() Examples

The following are code examples for showing how to use collections.ChainMap(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: quantiphy   Author: KenKundert   File: quantiphy.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
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 
Example 2
Project: aiotask-context   Author: Skyscanner   File: __init__.py    (license) View Source Project 6 votes vote down vote up
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() 
Example 3
Project: hakkuframework   Author: 4shadoww   File: misc.py    (license) View Source Project 6 votes vote down vote up
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
######################################################################## 
Example 4
Project: python-   Author: secondtonone1   File: compat.py    (license) View Source Project 5 votes vote down vote up
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 
Example 5
Project: python-   Author: secondtonone1   File: compat.py    (license) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 6
Project: python-   Author: secondtonone1   File: compat.py    (license) View Source Project 5 votes vote down vote up
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:]) 
Example 7
Project: python-   Author: secondtonone1   File: compat.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 8
Project: python-   Author: secondtonone1   File: compat.py    (license) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 9
Project: quantiphy   Author: KenKundert   File: quantiphy.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
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]
    ) 
Example 10
Project: my-first-blog   Author: AnkurBegining   File: compat.py    (license) View Source Project 5 votes vote down vote up
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 
Example 11
Project: my-first-blog   Author: AnkurBegining   File: compat.py    (license) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 12
Project: my-first-blog   Author: AnkurBegining   File: compat.py    (license) View Source Project 5 votes vote down vote up
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:]) 
Example 13
Project: my-first-blog   Author: AnkurBegining   File: compat.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 14
Project: my-first-blog   Author: AnkurBegining   File: compat.py    (license) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 15
Project: Daniel-Arbuckles-Mastering-Python   Author: PacktPublishing   File: async_trans.py    (MIT License) View Source Project 5 votes vote down vote up
def __aenter__(self):
        self.writes = dict()
        return ChainMap(self.writes, self) 
Example 16
Project: Daniel-Arbuckles-Mastering-Python   Author: PacktPublishing   File: trans.py    (MIT License) View Source Project 5 votes vote down vote up
def __enter__(self):
        self.writes = dict()
        return ChainMap(self.writes, self) 
Example 17
Project: Flask_Blog   Author: sugarguo   File: compat.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
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 
Example 18
Project: Flask_Blog   Author: sugarguo   File: compat.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 19
Project: Flask_Blog   Author: sugarguo   File: compat.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
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:]) 
Example 20
Project: Flask_Blog   Author: sugarguo   File: compat.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
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) 
Example 21
Project: Flask_Blog   Author: sugarguo   File: compat.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 22
Project: pip-update-requirements   Author: alanhamlett   File: compat.py    (BSD 2-Clause "Simplified" License) View Source Project 5 votes vote down vote up
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 
Example 23
Project: pip-update-requirements   Author: alanhamlett   File: compat.py    (BSD 2-Clause "Simplified" License) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 24
Project: pip-update-requirements   Author: alanhamlett   File: compat.py    (BSD 2-Clause "Simplified" License) View Source Project 5 votes vote down vote up
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:]) 
Example 25
Project: pip-update-requirements   Author: alanhamlett   File: compat.py    (BSD 2-Clause "Simplified" License) View Source Project 5 votes vote down vote up
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) 
Example 26
Project: pip-update-requirements   Author: alanhamlett   File: compat.py    (BSD 2-Clause "Simplified" License) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 27
Project: craton   Author: openstack   File: models.py    (license) View Source Project 5 votes vote down vote up
def resolved(self):
        """Provides a mapping that uses scope resolution for variables"""
        return ChainMap(*self.resolution_order_variables) 
Example 28
Project: swjtu-pyscraper   Author: Desgard   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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 
Example 29
Project: swjtu-pyscraper   Author: Desgard   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 30
Project: swjtu-pyscraper   Author: Desgard   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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:]) 
Example 31
Project: swjtu-pyscraper   Author: Desgard   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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) 
Example 32
Project: swjtu-pyscraper   Author: Desgard   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 33
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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 
Example 34
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 35
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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:]) 
Example 36
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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) 
Example 37
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 38
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 39
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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:]) 
Example 40
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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) 
Example 41
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 42
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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 
Example 43
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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:]) 
Example 44
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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) 
Example 45
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 46
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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 
Example 47
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def fromkeys(cls, iterable, *args):
            'Create a ChainMap with a single dict created from the iterable.'
            return cls(dict.fromkeys(iterable, *args)) 
Example 48
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
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) 
Example 49
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: compat.py    (MIT License) View Source Project 5 votes vote down vote up
def parents(self):                          # like Django's Context.pop()
            'New ChainMap from maps[1:].'
            return self.__class__(*self.maps[1:]) 
Example 50
Project: jira_worklog_scanner   Author: pgarneau   File: compat.py    (license) View Source Project 5 votes vote down vote up
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