def register_callable(self, key: Hashable):
"""
Decorator, that is invoked withe an identifiable `key` parameter, that
registers the callable that is decorated.
Example:
@registry.register_callable("DELETE")
def delete_command(options):
...
Args:
key: The Hashable key to use that identifies the callable in the
in the registry.
"""
def wrapper(f):
self.register(key, f)
@functools.wraps(f)
def wrapped(*args, **kwargs):
return f(*args, **kwargs)
return wrapped
return wrapper
python类Hashable()的实例源码
def get(self, key: Hashable) -> Any:
"""
Retrieves the value stored in the registry based on the `key` provided.
Args:
key: The key used to retrieve the stored object in the registry
Returns:
The object stored in the registry based on the `key` parameter
Raises:
RegistryKeyError: If the key was not found in the registry
"""
if key not in self._registry:
raise RegistryKeyError("Specified key: {} is not in the registry".format(key))
return self._registry[key]
def key(self) -> Hashable:
"""
Unique key for tracking this attachment. it will be generated during attachment process in
:meth:`.attach` method.
:type: Hashable
"""
return self.get('key')
def lru_dict_arg_cache(func: Callable) -> Callable:
# TODO? wrapper that allows maxsize
def unpacking_func(func: Callable, arg: frozenset) -> Any:
return func(dict(arg))
_unpacking_func = _lru_cache_wrapper(partial(unpacking_func, func), # type: ignore
64, False, _CacheInfo)
def packing_func(arg: Dict[Hashable, Hashable]) -> Any:
return _unpacking_func(frozenset(arg.items()))
update_wrapper(packing_func, func)
packing_func.cache_info = _unpacking_func.cache_info # type: ignore
return packing_func
def __init__(self: 'lazydict', value_factory: Callable[[Hashable], Any]=None, *args, **kw):
"""Initialize new lazy dictionary.
Keyword arguments:
value_factory: The callable to use for constructing
missing values.
Other arguments are the same as for built-in `dict`.
"""
self.value_factory = value_factory
super().__init__(None, *args, **kw)
def __missing__(self: 'lazydict', key: Hashable) -> Any:
"""Attempts to construct the value and inserts it into the dictionary."""
if self.value_factory is None:
raise KeyError(key)
value = self.value_factory(key)
self[key] = value
return value
def register(self, key: Hashable, value: Any):
"""
Registers any key / value combination.
Args:
key: The hashable key that identifies the object
value: The value to store in the registry
"""
self._registry[key] = value