Python cachetools.cached() Examples

The following are 12 code examples of cachetools.cached(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cachetools , or try the search function .
Example #1
Source File: toonapilib.py    From toonapilib with MIT License 6 votes vote down vote up
def thermostat_states(self):
        """The thermostat states of toon, cached for 1 hour."""
        url = '{api_url}/thermostat/states'.format(api_url=self._api_url)
        response = self._session.get(url)
        if response.status_code == 202:
            self._logger.debug('Response accepted but no data yet, '
                               'trying one more time...')
        response = self._session.get(url)
        try:
            states = response.json().get('state', [])
        except ValueError:
            self._logger.debug('No json on response :%s', response.text)
            raise IncompleteStatus

        return [ThermostatState(STATES[state.get('id')],
                                state.get('id'),
                                state.get('tempValue'),
                                state.get('dhw'))
                for state in states] 
Example #2
Source File: test_wrapper.py    From cachetools with MIT License 6 votes vote down vote up
def test_decorator_lock(self):
        class Lock(object):

            count = 0

            def __enter__(self):
                Lock.count += 1

            def __exit__(self, *exc):
                pass

        cache = self.cache(2)
        wrapper = cachetools.cached(cache, lock=Lock())(self.func)

        self.assertEqual(len(cache), 0)
        self.assertEqual(wrapper.__wrapped__, self.func)
        self.assertEqual(wrapper(0), 0)
        self.assertEqual(Lock.count, 2)
        self.assertEqual(wrapper(1), 1)
        self.assertEqual(Lock.count, 4)
        self.assertEqual(wrapper(1), 1)
        self.assertEqual(Lock.count, 5) 
Example #3
Source File: test_wrapper.py    From cachetools with MIT License 6 votes vote down vote up
def test_zero_size_cache_decorator_lock(self):
        class Lock(object):

            count = 0

            def __enter__(self):
                Lock.count += 1

            def __exit__(self, *exc):
                pass

        cache = self.cache(0)
        wrapper = cachetools.cached(cache, lock=Lock())(self.func)

        self.assertEqual(len(cache), 0)
        self.assertEqual(wrapper.__wrapped__, self.func)

        self.assertEqual(wrapper(0), 0)
        self.assertEqual(len(cache), 0)
        self.assertEqual(Lock.count, 2) 
Example #4
Source File: toonapilib.py    From toonapilib with MIT License 5 votes vote down vote up
def status(self):
        """The status of toon, cached for 300 seconds."""
        url = '{api_url}/status'.format(api_url=self._api_url)
        response = self._session.get(url)
        if response.status_code == 202:
            self._logger.debug('Response accepted but no data yet, '
                               'trying one more time...')
        response = self._session.get(url)
        try:
            data = response.json()
        except ValueError:
            self._logger.debug('No json on response :%s', response.text)
            raise IncompleteStatus
        return data 
Example #5
Source File: op_graph.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def tdcache():
    """
    Decorator to mark tensor description method as cached.

    Returns:
        Cache decorator set to use a particular cache.
    """
    return cachetools.cached(cache=tdcache.tensor_description_cache) 
Example #6
Source File: op_graph.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def forward(self):
        """
        If not None, self has been replaced with forward.

        When set, invalidates cached tensor descriptions.

        Returns:
             None or the replacement.
        """
        return self._forward 
Example #7
Source File: op_graph.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def all_deps(self):
        """
        TODO: use cached property as other Op
        """
        base_deps = super(ValueOp, self).all_deps
        if self.value_tensor is not None and self.value_tensor.is_device_op:
            # Add value_tensor if it is a real op
            return base_deps | OrderedSet([self.value_tensor])
        else:
            return base_deps 
Example #8
Source File: serialised_models.py    From notifications-api with MIT License 5 votes vote down vote up
def memory_cache(func):
    @cachetools.cached(
        cache=caches[func.__qualname__],
        lock=locks[func.__qualname__],
        key=ignore_first_argument_cache_key,
    )
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    return wrapper 
Example #9
Source File: test_wrapper.py    From cachetools with MIT License 5 votes vote down vote up
def test_decorator(self):
        cache = self.cache(2)
        wrapper = cachetools.cached(cache)(self.func)

        self.assertEqual(len(cache), 0)
        self.assertEqual(wrapper.__wrapped__, self.func)

        self.assertEqual(wrapper(0), 0)
        self.assertEqual(len(cache), 1)
        self.assertIn(cachetools.keys.hashkey(0), cache)
        self.assertNotIn(cachetools.keys.hashkey(1), cache)
        self.assertNotIn(cachetools.keys.hashkey(1.0), cache)

        self.assertEqual(wrapper(1), 1)
        self.assertEqual(len(cache), 2)
        self.assertIn(cachetools.keys.hashkey(0), cache)
        self.assertIn(cachetools.keys.hashkey(1), cache)
        self.assertIn(cachetools.keys.hashkey(1.0), cache)

        self.assertEqual(wrapper(1), 1)
        self.assertEqual(len(cache), 2)

        self.assertEqual(wrapper(1.0), 1)
        self.assertEqual(len(cache), 2)

        self.assertEqual(wrapper(1.0), 1)
        self.assertEqual(len(cache), 2) 
Example #10
Source File: test_wrapper.py    From cachetools with MIT License 5 votes vote down vote up
def test_decorator_typed(self):
        cache = self.cache(3)
        key = cachetools.keys.typedkey
        wrapper = cachetools.cached(cache, key=key)(self.func)

        self.assertEqual(len(cache), 0)
        self.assertEqual(wrapper.__wrapped__, self.func)

        self.assertEqual(wrapper(0), 0)
        self.assertEqual(len(cache), 1)
        self.assertIn(cachetools.keys.typedkey(0), cache)
        self.assertNotIn(cachetools.keys.typedkey(1), cache)
        self.assertNotIn(cachetools.keys.typedkey(1.0), cache)

        self.assertEqual(wrapper(1), 1)
        self.assertEqual(len(cache), 2)
        self.assertIn(cachetools.keys.typedkey(0), cache)
        self.assertIn(cachetools.keys.typedkey(1), cache)
        self.assertNotIn(cachetools.keys.typedkey(1.0), cache)

        self.assertEqual(wrapper(1), 1)
        self.assertEqual(len(cache), 2)

        self.assertEqual(wrapper(1.0), 2)
        self.assertEqual(len(cache), 3)
        self.assertIn(cachetools.keys.typedkey(0), cache)
        self.assertIn(cachetools.keys.typedkey(1), cache)
        self.assertIn(cachetools.keys.typedkey(1.0), cache)

        self.assertEqual(wrapper(1.0), 2)
        self.assertEqual(len(cache), 3) 
Example #11
Source File: test_wrapper.py    From cachetools with MIT License 5 votes vote down vote up
def test_zero_size_cache_decorator(self):
        cache = self.cache(0)
        wrapper = cachetools.cached(cache)(self.func)

        self.assertEqual(len(cache), 0)
        self.assertEqual(wrapper.__wrapped__, self.func)

        self.assertEqual(wrapper(0), 0)
        self.assertEqual(len(cache), 0) 
Example #12
Source File: float_ew2.py    From ngraph-python with Apache License 2.0 4 votes vote down vote up
def add_kernel(self, transformer, ops):
        assert not self.compiled
        # Take care tensor dimensionality
        ops = _wrap_tensor_descriptions(transformer, ops)

        # Generate kernel source code and block/grid mapping or find cached equivalent kernel
        (axes_mapping, dims) = _get_axes_mapping(ops)
        kernel_key = _ops_to_hash(ops, axes_mapping)
        if kernel_key in self.cache:
            kernel_name = self.cache[kernel_key]
            params = _generate_new_kernel_args(ops, axes_mapping, dims)
        else:
            code, kernel_name, arg_desc, params = _get_compound_kernel(ops, axes_mapping, dims,
                                                                       str(self.num_kernels))
            self.cache[kernel_key] = kernel_name

            # Add kernel code to source file
            self.buffer.write(code)

            # Save arg_desc in dict
            self.arg_descs[kernel_name] = arg_desc

            # Increment number of kernels
            self.num_kernels = self.num_kernels + 1

        # Calculate block and grid dims
        blockdim = [1, 1, 1]
        griddim = [1, 1, 1]
        for axis in axes_mapping:
            if axis[0] == 'x':
                blockdim[0] = axis[1]
                griddim[0] = axis[2]
            elif axis[0] == 'y':
                blockdim[1] = axis[1]
                griddim[1] = axis[2]
            elif axis[0] == 'z':
                blockdim[2] = axis[1]
                griddim[2] = axis[2]

        params = [tuple(griddim), tuple(blockdim), None] + params

        # Return kernel name and params
        return (kernel_name, params)