Module utilities.value_cache_dict

Useful in extending functionality for comprehensions by allowing the current iteration to reference the results of prior iteration(s). See integrators/docuvision_integrator.py for an example implementation.

Classes

class CacheDictCheck (check_name: str, key_arg: str | int, value_arg: str | int, key_check: collections.abc.Callable[[typing.Any], bool], format_key: collections.abc.Callable[[typing.Any], collections.abc.Hashable] = builtins.str, value_check: collections.abc.Callable[[typing.Any], bool] = <function CacheDictCheck.<lambda>>, preprocess_value: collections.abc.Callable[[typing.Any], typing.Any] = <function CacheDictCheck.<lambda>>, concat_value: collections.abc.Callable[[typing.Any, typing.Any], typing.Any] = <function CacheDictCheck.<lambda>>, format_arg_value: collections.abc.Callable[[typing.Any, typing.Any], typing.Any] = <function CacheDictCheck.<lambda>>, cache_factory: collections.abc.Callable[[], typing.Any] = builtins.str, reset_arg: str | int | None = None, *, reset_check: collections.abc.Callable[[typing.Any, typing.Any], bool] = <function CacheDictCheck.<lambda>>)

Defines a cache_check for use with the ValueCacheDict decorator.

Defines the tests for determining whether an arg or kwarg should be added to the cache, how the value should be formatted prior to being added, how to combine it with previously cached values, and how the value in the cache should be reincorporated into the values of the args and kwargs supplied in the call before they are forwarded to the wrapped function.

Attributes

check_name : str
key for this check's cache in the cache_dict of the @ValueCacheDict wrapped function.
key_arg : str | int
the argument to the wrapped function that will be used to generate the cache dict key. An int represents the zero based index of a postional arg (see note below). A str must reference a valid keyword argument.
value_arg : str | int
the argument to the wrapped function that will be used to create values for the cache.
key_check : Callable[[Any], bool]
given the resolved value of the 'key_arg' (see ValueCacheDict), return True to trigger caching.
format_key : Callable[[Any], Hashable]
given the resolved value of the 'key_arg', return a cache dict key. (Default is str)
value_check : Callable[[Any], bool]
given the resolved value of the 'value_arg' (see ValueCacheDict), return True to trigger caching. (Default is lambda _: True)
preprocess_value : Callable[[Any], Any]
given the resolved value of the 'value_arg', return a modified representation fit for caching. (Default is lambda x: x)
concat_value : Callable[[Any, Any], Any]
given the cached value and the preprocessed new value, return a new cached value. (Default is lambda x, y: x + y)
format_arg_value : Callable[[Any, Any], Any]
given the original resolved value of the 'value_arg' and the new cache value, return the value of 'value_arg' to use when calling the wrapped function. (Default is lambda: _, y: y (passes the new cache value))
cache_factory : Callable[[], Any]
assigned to the "default_factory" attribute of the internal defaultdict serving as the cache for the duration of this check. (Default is str)
reset_arg : str | int | None
the argument to the wrapped function that will be evaluated to determine whether the cache should be reset. If None, the cache will never reset.
reset_check : Callable[[Any, Any], bool]
given the previously resolved value of the 'reset_arg' and the current resolved value, return True to reset the cache. (Default is lambda x, y: x != y)

NOTE: the arg at position zero of a class or instance method will be "cls" or "self", so the arguments in the actual function call will begin at position 1 for those use cases.

Class variables

var cache_factory : collections.abc.Callable[[], typing.Any]

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var cached : dict[collections.abc.Hashable, typing.Any]
var check_name : str
var format_key : collections.abc.Callable[[typing.Any], collections.abc.Hashable]

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var key_arg : str | int
var key_check : collections.abc.Callable[[typing.Any], bool]
var reset_arg : str | int | None
var reset_value : Any
var value_arg : str | int

Methods

def concat_value(x, y) ‑> collections.abc.Callable[[typing.Any, typing.Any], typing.Any]
def format_arg_value(_, y) ‑> collections.abc.Callable[[typing.Any, typing.Any], typing.Any]
def preprocess_value(x) ‑> collections.abc.Callable[[typing.Any], typing.Any]
def reset_check(x, y) ‑> collections.abc.Callable[[typing.Any, typing.Any], bool]
def value_check(_) ‑> collections.abc.Callable[[typing.Any], bool]
class ValueCacheDict (cache_checks: collections.abc.Sequence[CacheDictCheck], **kwargs)

A decorator proviing flexible caching options for use in comprehensions.

Initially developed to provide a means to cache the parts of a full address when street address and city/state/zip are found in different sections of a form and thus labeled with independent bounding boxes, this class and its supporting class CacheDictCheck are implemented for general use. Useful in extending the basic functionality of comprehensions by allowing the current iteration to reference the results of prior iteration(s).

Args

cache_checks : Sequence[CacheDictCheck]
one CacheDictCheck instance per cached parameter. See CacheDictCheck for details.
ex_handler : Callable[[Exception], bool]
a user defined function to which all raised exceptions are passed. Return True to continue processing. Return False (default) to raise the exception to the calling thread. Default is lambda _: False.

Attributes

cache_dict : dict[Any, Any]
this attribute will be added to functions decorated with this class to provide convenient access to the cached attributes of each CacheDictCheck.

Instance variables

prop cache_dict : dict[collections.abc.Hashable, dict[collections.abc.Hashable, typing.Any]]

dict of dicts of form {'[check.check_name]: {[check.cached]}'