本文整理汇总了Python中cachetools.cached方法的典型用法代码示例。如果您正苦于以下问题:Python cachetools.cached方法的具体用法?Python cachetools.cached怎么用?Python cachetools.cached使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cachetools
的用法示例。
在下文中一共展示了cachetools.cached方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: thermostat_states
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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]
示例2: test_decorator_lock
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)
示例3: test_zero_size_cache_decorator_lock
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)
示例4: status
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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
示例5: tdcache
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)
示例6: forward
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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
示例7: all_deps
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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
示例8: memory_cache
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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
示例9: test_decorator
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)
示例10: test_decorator_typed
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)
示例11: test_zero_size_cache_decorator
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)
示例12: add_kernel
# 需要导入模块: import cachetools [as 别名]
# 或者: from cachetools import cached [as 别名]
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)