cached#
import ampform.sympy.cached
Handy aliases for working with cached SymPy expressions.
- doit(expr: SympyObject) SympyObject[source]#
Perform
doit()and cache the result to disk.The cached result is fetched from disk if the hash of the original expression is the same as the hash embedded in the filename (see
get_readable_hash()).- Parameters:
expr – A
sympy.Expron which to calldoit().
Added in version 0.14.4.
Helper functions for
cached.doit()and related functions.These methods are private, but can be imported from this module:
import ampform.sympy._cache
- cache_to_disk(func: Callable[P, T]) Callable[P, T][source]#
- cache_to_disk(*, dump_function: Callable[[Any, SupportsWrite[bytes]], None] = pickle.dump, load_function: Callable[[BufferedReader], Any] = pickle.load, dependencies: list[str] | None = None, function_name: str | None = None) Callable[[Callable[P, T]], Callable[P, T]]
Decorator for caching the result of a function to disk.
This function works similarly to
functools.cache, but it stores the result of the function to disk as a pickle file.Tip
Caching can be disabled by setting the environment variable
NO_CACHE. This can be useful to test if caches are correctly invalidated.Set
COMPWA_CACHE_DIRto change the cache directory. Alternatively, have a look at the implementation ofget_system_cache_directory()to see how the cache directory is determined from system environment variables.
- get_system_cache_directory() str[source]#
Return the system cache directory for the current platform.
>>> import sys >>> if sys.platform.startswith("darwin"): ... assert get_system_cache_directory().endswith("/Library/Caches") >>> if sys.platform.startswith("linux"): ... assert get_system_cache_directory().endswith("/.cache") >>> if sys.platform.startswith("win"): ... assert get_system_cache_directory().endswith(R"\AppData\Local")
- get_readable_hash(obj: Hashable, /) str[source]#
Get a human-readable hash of any hashable Python object.
The hash follows from the value of the object, not from the identity of the parts it is built from. Two SymPy expressions that compare equal therefore hash the same, no matter what was constructed before them or in which process, which is what makes the hash usable as a cache key in
cache_to_disk().A
setordicthanded to this function directly is serialized in iteration order, which depends onPYTHONHASHSEEDwhen its elements or keys arestr. Pass it throughmake_hashable()first, ascache_to_disk()does, to put it in a fixed order.- Parameters:
obj – Any hashable object, mutable or immutable, to be hashed.
- to_bytes(obj, /) bytes[source]#
Convert any Python object to
byteswithpickle.The bytes depend on the value of the object rather than on which of its parts happen to be the same instance. See
get_readable_hash()for what that guarantees.
- make_hashable(*args) Hashable[source]#
Make a hashable object from any Python object.
Sets and dictionaries are put in a fixed order, because a
setiterates in an order that depends onPYTHONHASHSEEDforstrelements, and adictbuilt by iterating one inherits that order. A cache key built from them would otherwise differ in every process.>>> make_hashable("a", 1, {"b": 2}, {3, 4}) ('a', 1, frozendict.frozendict({'b': 2}), _SortedSet(items=(3, 4))) >>> make_hashable({"a": {"sub-key": {1, 2, 3}, "b": [4, 5]}}) frozendict.frozendict({'a': frozendict.frozendict({'b': (4, 5), 'sub-key': _SortedSet(items=(1, 2, 3))})}) >>> make_hashable("already-hashable") 'already-hashable'
- simplify(expr: Expr, *args, **kwargs) Expr[source]#
Perform
simplify()and cache the result to disk.Added in version 0.15.7.
- trigsimp(expr: Expr, *args, **kwargs) Expr[source]#
Perform
trigsimp()and cache the result to disk.Added in version 0.15.7.
- subs(expr: Expr, substitutions: Mapping[Basic, Any]) Expr[source]#
Call
subs()and cache the result to disk.The order of the substitutions does not affect the cache key.
- xreplace(expr: Expr, substitutions: Mapping[Basic, Any]) Expr[source]#
Call
xreplace()and cache the result to disk.The order of the substitutions does not affect the cache key.