Skip to main content

Cache Return

This library provide a simple wrapper for custom functions to get the caching mechanism.

Use cases and benefits

With the caching mechanism activated, the result of a sub-function will be cached in the dedicated folder in the initial run. And the in the following runs, the cached result will loaded to bypass the actual internal process of a function.

This will be mainly useful for testing or debug tasks of larger projects with sophisticated sub-functions. As it will

  1. Avoid the actual interanl function run, thus save resources (e.g. queries/calls to APIs or databases) and improve efficiency.
  2. Achieve quicker testing processes and circles, as the reloading process is generally much faster than the function run.
  3. Provide the possibility for "offline" debugging and investigation for the real cases, as the cached return result can be manually accessed for investigation.

Examples

To add caching mechanism to a custom function, simply add the decorator wrapper to the function definition as below.

from cache_return import cache_return

@cache_return
def custom_function(arg_a, arg_b='default_value'):
    ... internal processes ...
    return results

Then custom function can be usage the same way as before. To activate its caching mechanism, simply provide an additional keyword argument caching=True.

# Before

return_result = custom_function(arg_a, arg_b='actual_value')

# After

return_result = custom_function(arg_a, arg_b='actual_value', caching=True)

To turn the caching mechanism off in the production code, you can either set the keyword argument as caching=False, or simple remove the caching=True keyword argument, as the default value caching=False will be used in this case.

The actual cached result will be saved in the auto-created folder ./_cache_ (sitting at the same directory as the top-level running script, not the script containing the custom function.), with the same name of the custom function as a pickle file

Then the cached result can be manully accessed in other places by the code below

import pickle

with open('./_cache_/custom_function.pkl', 'rb') as f:
    return_result = pickle.load(f)

Special note for environment with pandas version >= 2.0.0

The above custom investigation code has to be adjusted to the below code as

import pandas as pd

with open('./_cache_/custom_function.pkl', 'rb') as f:
    return_result = pd.compat.pickle_compat.load('./_cache_/custom_function.pkl') 

Or, you can downgrade pandas to the 1.x series by pip install pandas<2.0.0, see the stackoverflow topic here.

Additional arguments

In some cases, if you only want to overwrite/update the cached result in the new function run, you can achieve this by set the flushing keyword argument as flushing=True.

By default the cached result will be save under directory ./_cache_. An alternative directory custom_dir can be set by the argument cache_path as cache_path=custom_dir. Such a directory will be automatically created if it does not exist.

Metadata

Release files for cache-return 0.1.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 cache-return 0.1.2
File Size Uploaded Provenance
cache_return-0.1.2.tar.gz 4.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cache-return 0.1.2
File Interpreter ABI Platform
cache_return-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / cache_return-0.1.2.tar.gz

Download URL cache_return-0.1.2.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
99484947a43da9dd7ee5e78ddd8db072a684c99f17fb3438cac08d005e269c7e
BLAKE2b-256 checksum
How to use checksums
1b6b55112c0d6ee0689bb17552cb6089a86d2e0968b7b08eca18d3c0b2d47e00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Release files / cache_return-0.1.2-py3-none-any.whl

Download URL cache_return-0.1.2-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
649d8bc4b6a6b5a6247c29f131099882aeefa708a9b49d03a0c926ff9b9df748
BLAKE2b-256 checksum
How to use checksums
29fbc1fced10a8d5fe07fcba3259369df5fb4a0bc9e5899d814ea7488c662031
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Security for cache-return 0.1.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.

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