Skip to main content

Scrat

Persistent Caching of Expensive Function Results

🐿️

Get Started

  1. Install with pip install scrat
  2. Initialize stash scrat init
  3. Start saving time:
import scrat
import time

@scrat.stash()
def expensive_function(param_1):
    time.sleep(3)
    return param_1

expensive_function(1)  # <- function called
expensive_function(1)  # <- function not called the result is recovered from stash
expensive_function(2)  # <- function called again beacuse the parameters changed

Features

  • Seamlessly stores the results of expensive functions to disk for future reuse.
  • Automatically re-evaluates the function if the parameters or function code have changed, ensuring up-to-date results.
  • Saves any result using the pickle.
  • Improved storage of pandas DataFrames, Series, and Numpy arrays.
  • Customizable support for alternative serializers.
  • Flexible parameter hashing mechanism to efficiently handle any parameter type.
  • Command-line interface (CLI) for convenient control and management of the caching functionality.

Similar Projects

lru_cache

Great and fast memoize provided by the standard library functools, unfurtunately results are stored in memory so they can't be reused in different runs.

cachetools

Provides alternatives to lru_cache but it also works in-memory.

Joblib

Joblib is a stablished library that provides great functionality for parallelization and caching. The Memory module provides an excelent alternative to Scrat, but it does have some limitations:

  • Hard to avoid using pickle
  • Lack of options to control the cache size and policies
  • Lack of tools to inspect and cleanup the cache

These are the problems that scrat aims to improve, however, I'd recommend using Joblib in production since it's much more mature than Scrat at the moment.

Concepts

  • Scrat is a famous pre-historic squirrel with some bad luck
  • Stash is composed of a folder where results are saved and a database to index them
  • A Nut is one of the entries in the database
  • The Squirrel is in charge of fetching and stashing the Nuts
  • Serializer dumps results to files and load them back to memory
  • Hasher creates unique hashes for a parameter value
  • HashManager coordinates hashes of all arguments and functon code

Development Setup

  1. Clone this repo
  2. Install pyenv.
  3. Install the python version used for development running pyenv install in the root of this repository.
  4. Install poetry. Version 1.5.1 is recommended.
  5. Run this command to make sure poetry uses the right python version poetry env use $(which python)
  6. Install project and dependencies with poetry install
  7. Run tests with poetry run pytest or activate the virtualenv with poetry shell and then run pytest

Metadata

Release files for scrat 0.3.0

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

Source distribution (sdist)

Source distribution for scrat 0.3.0
File Size Uploaded
scrat-0.3.0.tar.gz 17.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scrat 0.3.0
File Interpreter ABI Platform
scrat-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 40.4 kB

Release files / scrat-0.3.0.tar.gz

Download URL scrat-0.3.0.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e433d0c548fbbc585f5bd54f655571c42792572edf49c0572e362665e0c30dc4
BLAKE2b-256 checksum
How to use checksums
443ed23dc47c36891f028cfcd9995be891aba03bab32872b30fb8b54d9312434
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Darwin/22.5.0

Release files / scrat-0.3.0-py3-none-any.whl

Download URL scrat-0.3.0-py3-none-any.whl
Size 23.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c39395831672b63f997253295d4ac98770ff12ff7bf4a64828ae671a18046614
BLAKE2b-256 checksum
How to use checksums
65eeb33d363bd8351ebcea6a6017065321e1a92b65284be5b2b248afe577abf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Darwin/22.5.0

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.1.0

2 release files

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