Durable memoization that refreshes on code updates
Project description
astrocache 
Durable memoization that automatically refreshes as you update your source code.
Installation
Requires Python >= 3.9.
python3 -m pip install astrocache
What is this?
This library provides disk-backed memoization for Python functions with a surprisingly useful twist: the cache is sensitive not only to your function's inputs, but also to its implementation (represented by its abstract syntax tree or AST).
If you're unfamiliar with memoization, check out the Wikipedia page and functools.cache for in-depth explanations.
AST-sensitive memoization
import astrocache
@astrocache.cache()
def foo(a, b):
return slow_fn(a) + b
This creates a disk-backed cache for foo, which prevents foo from performing the
same work more than once for a given input. So far, this is pretty standard; many
other libraries offer the same thing.
But what if you change the behavior of foo?
@astrocache.cache()
def foo(a, b):
return slow_fn(a) + b * 2
Your cache entries are no longer valid.
Luckily, astrocache is aware of this. It will create new cache entries for the new
version of foo.
This treatment extends recursively to any function called by foo as well. In this
case, changes to slow_fn (and to functions called by slow_fn, etc) will also
invalidate cache entries for foo.
How does it work?
Astrocache creates a fingerprint of your function's abstract syntax tree (AST) using the ast module from the standard library.
When your function is called, this fingerprint is combined with the provided arguments to create a cache key.
How is this useful?
This is particularly useful in highly interactive workflows, e.g. during rapid iteration or in a notebook setting. Many libraries provide some form of memoization, but none (as far as I know) manage this kind of cache invalidation for you.
As an example, imagine you're rapidly iterating on a script that processes data in several expensive steps, or hits a usage-capped external API.
Memoization could certainly make you more productive, but you'd have to remember to clear the various cache entries as you updated your code.
This library automates this for you, allowing you to take advantage of memoization without needing to worry about clearing the cache.
Limitations
Referenced functions
There is a caveat regarding AST inspection of functions that are referenced, but not invoked. Example:
import astrocache
def bar(a):
return a + 1
@astrocache.cache()
def foo(x):
return baz(bar)
Here, bar is referenced (passed as an argument), but not invoked, by foo.
Functions appearing within your cached function are only included in the AST fingerprint if your cached function does any of these:
- Calls the referenced function
- Receives the referenced function as an argument
- Contains the definition of the referenced function
Here are some examples to illustrate this:
✅ Calling a function
@astrocache.cache()
def foo(referenced_function):
# referenced_function will be included
referenced_function(1)
✅ Receiving the function as an argument
@astrocache.cache()
def foo(referenced_function):
# referenced_function will be included
foo(referenced_function)
✅ Defining a function within the cached function
@astrocache.cache()
def foo():
# referenced_function will be included
def referenced_function():
return 1
foo(referenced_function)
❌ Assigning a function from outer scope to a variable (only)
@astrocache.cache()
def foo():
# referenced_function will NOT be included
foo = referenced_function
❌ Passing a function from outer scope to a called function (only)
@astrocache.cache()
def foo():
# referenced_function will NOT be included
foo(referenced_function)
Related projects
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file astrocache-0.0.4.tar.gz.
File metadata
- Download URL: astrocache-0.0.4.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f9bff8a501c04b74bbef5f32e9396f0e24eac934244c639f511b8aa58f14959
|
|
| MD5 |
7b3ab8a1e63269d1cd521479f64d2937
|
|
| BLAKE2b-256 |
9f3fe7c2b0866c95488969a63184c2b16faa121603ae0907a64fee9beef82771
|
File details
Details for the file astrocache-0.0.4-py3-none-any.whl.
File metadata
- Download URL: astrocache-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a6a2e588685c5da5d04d6274dc8a857aaa9cd87061ad3bdd3c7f3509159a240
|
|
| MD5 |
f5ae62074cd9e33aac8417b4a3911537
|
|
| BLAKE2b-256 |
9892836095a41c1070159e462ec9cddee9ac88d06b32baae2b8278acbff6ce03
|