Skip to main content

functionfaker

Lightweight Decorator to cache (memoize) function calls and replay responses for expensive computations and API requests.

Pip installable via pip install functionfaker

Functionfaker offers lightweight and easy to understand function (and method) caching, similar to some of the functionality of the Joblib package. A function response is recorded once and then replayed from cache from thereon. This allows for unit testing applications with API calls without actaully calling the API. It can also speed up prototyping of computationally expensive code. Functionfaker consists of simple code, and provides a single decorator for your functions and methods, as shown in the following "hello world" example.

from functionfaker import response_player
import os

Add the response_player decorator to an example function called add:

@response_player()
def add(x, y):
    return x + y

Then set RECORD mode by setting the environment variable:

# enter record mode, to record function responses.
os.environ['RECORD'] = "record"

Call the add function a few times:

# Clear the stored function responses
if os.path.exists('responses.p'):
    os.remove('responses.p')
# call the add function to record some responses.
add(1,2)
add(1,y=3)
add(1,3)
add(2,1)
Recording response function "add"
Recording response function "add"
Recording response function "add"

Set replay mode via the environment variable:

# enter replay mode, so that function will not run, but return stored values instead.
os.environ['RECORD'] = "replay"

Call the add function again, with arguments that it has already seen:

result = add(1,2)
print("The saved result of adding 1 and 2 using function 'add' is %d"%result)
Faking function "add". Response found
The saved result of adding 1 and 2 using function 'add' is 3

The outputs for these inputs (1,2) are now read from cache.

Some funtion arguments might be irrelevant or difficult to serialize. To ignore these arguments, provide the args2ignore argument as a list of integers to the response_player decorator, where the integers represent the index in the argument list.

Default function response storage is in a simple Pickle file responses.p. To use your own storage system, provide a class derived from BaseStore class with an update and get_response method. An object of this class is then provided as the store argument to the response_player decorator.

Metadata

Release files for functionfaker 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for functionfaker 0.2
File Size Uploaded Provenance Actions
functionfaker-0.2.tar.gz 4.4 kB Without provenance Details for functionfaker-0.2.tar.gz

Release files / functionfaker-0.2.tar.gz

Download URL functionfaker-0.2.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c9af20400c61403cbd975e57fda4be9c5b10c906cecf81f75a8f6dca97b6133e
BLAKE2b-256 checksum
How to use checksums
f877b7d3c2ba3b0cb486ef631aa3be3f6a5097f1c605c4948c2e331bc5147c02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Security for functionfaker 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page