How to use the boltons.cacheutils.LRU function in boltons

To help you get started, we’ve selected a few boltons examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mahmoud / boltons / tests / test_cacheutils.py View on Github external
def test_unscoped_cached_dec():
    lru = LRU()
    inner_func = CountingCallable()
    func = cached(lru)(inner_func)

    other_inner_func = CountingCallable()
    other_func = cached(lru)(other_inner_func)

    assert inner_func.call_count == 0
    func('a')
    assert inner_func.call_count == 1
    func('a')

    other_func('a')
    assert other_inner_func.call_count == 0
    return
github mahmoud / boltons / tests / test_cacheutils.py View on Github external
def test_lru_add():
    cache = LRU(max_size=3)
    for i in range(4):
        cache[i] = i
    assert len(cache) == 3
    assert 0 not in cache
github mahmoud / boltons / tests / test_cacheutils.py View on Github external
def test_callable_cached_dec():
    lru = LRU()
    get_lru = lambda: lru

    inner_func = CountingCallable()
    func = cached(get_lru)(inner_func)

    assert inner_func.call_count == 0
    func()
    assert inner_func.call_count == 1
    func()
    assert inner_func.call_count == 1

    lru.clear()

    func()
    assert inner_func.call_count == 2
    func()
github mahmoud / boltons / tests / test_cacheutils.py View on Github external
lru['yet_another'] = 3
    assert lru.pop('yet_another', default) == 3

    lru['yet_another'] = 4
    lru.clear()
    assert not lru

    lru['yet_another'] = 5
    second_lru = LRU(max_size=1)
    assert lru.copy() == lru

    second_lru['yet_another'] = 5
    assert second_lru == lru
    assert lru == second_lru

    lru.update(LRU(max_size=2, values=[('a', 1),
                                       ('b', 2)]))
    assert len(lru) == 1
    assert 'yet_another' not in lru

    lru.setdefault('x', 2)
    assert dict(lru) == {'x': 2}
    lru.setdefault('x', 3)
    assert dict(lru) == {'x': 2}

    assert lru != second_lru
    assert second_lru != lru
github insilichem / gaudi / gaudi / genes / molecule.py View on Github external
def __init__(self, path=None, symmetry=None, hydrogens=False, pdbfix=False, vdw_radii=None, **kwargs):
        self._kwargs = kwargs.copy()
        GeneProvider.__init__(self, **kwargs)
        self._kwargs = kwargs
        self.path = path
        self.symmetry = symmetry
        self.hydrogens = hydrogens
        self.pdbfix = pdbfix
        self.vdw_radii = vdw_radii
        try:
            self.catalog = self._CATALOG[self.name]
        except KeyError:
            self.catalog = self._CATALOG[self.name] = tuple(self._compile_catalog())
        self._compounds_cache = self._cache.setdefault(self.name + '_compounds', LRU(300))
        self._atomlookup_cache = self._cache.setdefault(self.name + '_atomlookup', LRU(300))
        self._residuelookup_cache = self._cache.setdefault(self.name + '_residuelookup', LRU(300))
        self.allele = random.choice(self.catalog)

        # An optimization for similarity methods: xform coords are
        # cached here after all genes have expressed. See Individual.express.
        self._expressed_coordinates = None
github bluesky / databroker / databroker / assets / base_registry.py View on Github external
# set up the initial handler registry
        self.handler_reg = _ChainMap(handler_reg or {})

        # set up the initial root_map
        self.root_map = root_map or {}

        # ## set up the caches
        def _r_on_miss(k):
            col = self._resource_col
            ret = self._api.resource_given_uid(col, k)
            if ret is None:
                raise RuntimeError('did not find resource {!r}'.format(k))
            return ret

        self._datum_cache = boltons.cacheutils.LRU(max_size=1000000)
        self._handler_cache = boltons.cacheutils.LRU()
        self._resource_cache = boltons.cacheutils.LRU(on_miss=_r_on_miss)

        # copy the class level known spec to an instance attribute
        self.known_spec = dict(self.KNOWN_SPEC)
github bluesky / databroker / databroker / assets / base_registry.py View on Github external
self.handler_reg = _ChainMap(handler_reg or {})

        # set up the initial root_map
        self.root_map = root_map or {}

        # ## set up the caches
        def _r_on_miss(k):
            col = self._resource_col
            ret = self._api.resource_given_uid(col, k)
            if ret is None:
                raise RuntimeError('did not find resource {!r}'.format(k))
            return ret

        self._datum_cache = boltons.cacheutils.LRU(max_size=1000000)
        self._handler_cache = boltons.cacheutils.LRU()
        self._resource_cache = boltons.cacheutils.LRU(on_miss=_r_on_miss)

        # copy the class level known spec to an instance attribute
        self.known_spec = dict(self.KNOWN_SPEC)
github bluesky / databroker / databroker / eventsource / shim.py View on Github external
def _external_keys(descriptor, _cache=boltons.cacheutils.LRU(max_size=500)):
    """Which data keys are stored externally

    Parameters
    ----------
    descriptor : Doct
        The descriptor

    _cache : Mapping
        Cache to use.  Defaults to a boltons LRU closed over via
        mutable defaults.

    Returns
    -------
    external_keys : dict
        Maps data key -> the value of external field or None if the
        field does not exist.
github insilichem / gaudi / gaudi / genes / mutamers.py View on Github external
self.mutations = mutations
        self.ligation = ligation
        self.hydrogens = hydrogens
        self.avoid_replacement = avoid_replacement
        self.allele = []
        # set caches
        try:
            self.residues = self._cache[self.name + '_residues']
        except KeyError:
            self.residues = self._cache[self.name + '_residues'] = OrderedDict()
            
        try:
            self.rotamers = self._cache[self.name + '_rotamers']
        except KeyError:
            cache_size = len(residues) * (1 + 0.5 * len(mutations))
            self.rotamers = self._cache[self.name + '_rotamers'] = LRU(int(cache_size))

        if self.ligation:
            self.random_number = random.random()
        else:
            self.random_number = None

        # Avoid unnecessary calls to expensive get_rotamers if residue is known
        # to not have any rotamers
        self._residues_without_rotamers = ['ALA', 'GLY']
github mahmoud / boltons / boltons / cacheutils.py View on Github external
def __setitem__(self, key, value):
        with self._lock:
            try:
                link = self._get_link_and_move_to_front_of_ll(key)
            except KeyError:
                if len(self) < self.max_size:
                    self._set_key_and_add_to_front_of_ll(key, value)
                else:
                    evicted = self._set_key_and_evict_last_in_ll(key, value)
                    super(LRU, self).__delitem__(evicted)
                super(LRU, self).__setitem__(key, value)
            else:
                link[VALUE] = value