fleche
lru_cache on ‘roids.
Submodules
Attributes
Classes
A plain callable that freezes cache and metadata state at construction time. |
|
Type wrapper to mark a function argument as ignored for caching. |
|
Type wrapper to mark a function argument as required for caching. |
Functions
|
Manages the active cache for Fleche. |
|
Manages the active metadata for Fleche. |
|
A context manager to add arbitrary tags to results. |
|
A context manager to tag results with a project name. |
|
Cache decorator for functions. |
|
Monkey-patch |
|
Convenience wrapper to create a Digest from a value. |
Package Contents
- fleche.cache(new_cache: None = None, stack: bool = False) fleche.caches.BaseCache[source]
- fleche.cache(new_cache: fleche.caches.BaseCache | str, stack: bool = False) contextlib.AbstractContextManager[None]
Manages the active cache for Fleche.
If
new_cacheisNone, returns the currently active cache.Otherwise, immediately sets
new_cacheas the active cache and returns a context manager. When used in awithstatement the previous cache is restored on exit; when the returned context manager is discarded the new cache remains active (sticky behaviour).- Parameters:
new_cache – Cache object or named cache string to activate, or
Noneto query. The strings'memory'and'void'return transient backends regardless of configuration. The string'default'activates whichever cache the config file designates as the default — note that this is not the same as passingNone, which returns the currently active cache without changing anything.stack – If
True, wrapnew_cachein aCacheStackon top of the current cache.
- Returns:
The current
BaseCachewhen called without arguments, otherwise a_StickyContextcontext manager.
- fleche.meta(*new_metadata: fleche.metadata.MetaData, stack=False) contextlib.AbstractContextManager[None][source]
Manages the active metadata for Fleche.
Immediately sets
new_metadataas the active metadata and returns a context manager. When used in awithstatement the previous metadata is restored on exit; when the returned context manager is discarded the new metadata remains active (sticky behaviour).- Parameters:
*new_metadata –
MetaDatainstances to activate.stack – If
True, prepend the current metadata tuple before the new entries.
- Returns:
A
_StickyContextcontext manager.
- fleche.tags(**kwargs)[source]
A context manager to add arbitrary tags to results.
- Parameters:
**kwargs – The tags to add to the results.
- fleche.project(name)[source]
A context manager to tag results with a project name.
- Parameters:
name (str) – The name of the project.
- class fleche.BoundWrapper[source]
A plain callable that freezes cache and metadata state at construction time.
BoundWrapperis intentionally a minimal wrapper: it captures the activeBaseCacheand metadata tuple and restores them around every call to the wrapped function, but it does not expose theflechehelper namespace (digest,call,load,contains,query,rerun). Those helpers are available on the original decorated function.This is intended to enable passing around fleche-decorated functions in pickled form by baking the active state into the object.
- func: Callable
- cache: fleche.caches.BaseCache
- meta: tuple[fleche.metadata.MetaData, Ellipsis]
- classmethod bind(func)[source]
Bind cache and metadata state.
Returns a plain callable that always executes as if called under the context in which
bind()was originally invoked. The returned object is aBoundWrapper— a simple dataclass with a__call__method — and does not carry theflechehelper namespace. To access helpers such asdigestorquery, use them on the original decorated function.- Parameters:
func (callable) – any callable; plain functions that only call fleche-wrapped ones are explicitly allowed
- Returns:
instance with the bound cache and metadata state
- Return type:
- fleche.fleche(_func=None, *, version: str | int | None = None, meta: tuple[fleche.metadata.MetaData, Ellipsis] = (), hash_version: bool = True, hash_module: bool = True, hash_code: bool = False, require: None | str | list[str] | tuple[str] = None, ignore: None | str | list[str] | tuple[str] = None, isolate: bool = False)[source]
Cache decorator for functions.
The decorated function is enhanced with helper methods, accessible either directly on the wrapped function or bundled under a
.flechetypes.SimpleNamespace(e.g.f.fleche.call(...)is equivalent tof.call(...)):.query(*args, **kwargs): Return matching calls from the active cache.
.bind(args, **kwargs): Create a :class:`.BoundWrapper` that freezes the current cache/metadata state. Optionally pre-applies *args/kwargs via
functools.partial().
The original function is available via .__wrapped__.
Warning
isolate=Trueis not thread-safe. Internally it callsos.chdir(), which is a process-wide POSIX syscall shared by all threads. Concurrent calls withisolate=Truefrom multiple threads will clobber each other’s working directory, and a thread may find its temporary directory deleted before it has finished using it.Use
isolate=Trueonly from a single thread, or run isolated calls in separate processes (e.g. viaconcurrent.futures.ProcessPoolExecutor) where each process has its own working directory.
- class fleche.Ignored[source]
Type wrapper to mark a function argument as ignored for caching.
Can be used as a type hint:
arg: fleche.Ignoredorarg: fleche.Ignored[int].
- class fleche.Required[source]
Type wrapper to mark a function argument as required for caching.
Arguments marked as required must be explicitly provided by the caller as keyword arguments (i.e. not via their default value) for the result to be cached. This is useful for arguments like random seeds or iteration counts, where using the default value might lead to non-deterministic or otherwise undesirable caching behavior.
This is mainly useful when wrapping third-party functions where you do not control the default arguments.
Can be used as a type hint:
arg: fleche.Requiredorarg: fleche.Required[int].
- fleche.wrap_executor(executor)[source]
Monkey-patch
executor.submitto intercept fleche-wrapped functions.Calling
wrap_executor()on an executor that is already wrapped is a no-op: the patch is not stacked, and the originalsubmitcontinues to refer to the pre-wrap method.- Parameters:
executor – any object with a
submit(func, *args, **kwargs)method (e.g.concurrent.futures.Executorsubclass instances).- Returns:
The same
executorinstance, with a replacedsubmitattribute.
- fleche.D(value) digest.Digest[source]
Convenience wrapper to create a Digest from a value.
If given a non-empty string that is a valid hexadecimal string of at most
DIGEST_LENGTHcharacters, wraps it in aDigestdirectly (allowing short hex prefixes for use withexpand()). For any other value — including strings that are not valid hex digests — computes the digest.Warning
Passing an arbitrary string to
D()will silently compute its digest rather than treating the string as a digest identifier. Only strings that consist entirely of hexadecimal characters (0-9,a-f,A-F) and are no longer thanDIGEST_LENGTHcharacters are used verbatim. If you intend to look up a cached value by its digest string, make sure the string you pass satisfies those constraints; otherwise you will get the digest of the string itself, which is almost certainly not what you want.Digests passed as arguments to @fleche decorated functions are automatically expanded to their cached values.