scrat


Namescrat JSON
Version 0.3.0 PyPI version JSON
download
home_pagehttps://github.com/javiber/scrat
SummaryEasily stash the results of expensive functions to disk
upload_time2024-01-06 16:29:49
maintainer
docs_urlNone
authorjaviber
requires_python>=3.8,<4.0
licenseBSD-3-Clause
keywords cache disk
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Scrat

Persistent Caching of Expensive Function Results

![🐿️](docs/imgs/scrat.png)

## Get Started

1. Install with `pip install scrat`
2. Initialize stash `scrat init`
3. Start saving time:
``` python
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](https://github.com/pyenv/pyenv#installation).
3. Install the python version used for development running `pyenv install` in the root of this repository.
4. Install [poetry](https://python-poetry.org/docs/#installation). 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`

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/javiber/scrat",
    "name": "scrat",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "cache,disk",
    "author": "javiber",
    "author_email": "javier.berneche@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/44/3e/d23dc47c36891f028cfcd9995be891aba03bab32872b30fb8b54d9312434/scrat-0.3.0.tar.gz",
    "platform": null,
    "description": "# Scrat\n\nPersistent Caching of Expensive Function Results\n\n![\ud83d\udc3f\ufe0f](docs/imgs/scrat.png)\n\n## Get Started\n\n1. Install with `pip install scrat`\n2. Initialize stash `scrat init`\n3. Start saving time:\n``` python\nimport scrat\nimport time\n\n@scrat.stash()\ndef expensive_function(param_1):\n    time.sleep(3)\n    return param_1\n\nexpensive_function(1)  # <- function called\nexpensive_function(1)  # <- function not called the result is recovered from stash\nexpensive_function(2)  # <- function called again beacuse the parameters changed\n```\n\n## Features\n\n- Seamlessly stores the results of expensive functions to disk for future reuse.\n- Automatically re-evaluates the function if the parameters or function code have changed, ensuring up-to-date results.\n- Saves any result using the pickle.\n- Improved storage of pandas DataFrames, Series, and Numpy arrays.\n- Customizable support for alternative serializers.\n- Flexible parameter hashing mechanism to efficiently handle any parameter type.\n- Command-line interface (CLI) for convenient control and management of the caching functionality.\n\n\n## Similar Projects\n\n### lru_cache\nGreat and fast memoize provided by the standard library `functools`, unfurtunately results are stored in memory so they can't be reused in different runs.\n\n### cachetools\nProvides alternatives to `lru_cache` but it also works in-memory.\n\n### Joblib\n\nJoblib 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:\n- Hard to avoid using pickle\n- Lack of options to control the cache size and policies\n- Lack of tools to inspect and cleanup the cache\n\nThese 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.\n\n## Concepts\n- `Scrat` is a famous pre-historic squirrel with some bad luck\n- `Stash` is composed of a folder where results are saved and a database to index them\n- A `Nut` is one of the entries in the database\n- The `Squirrel` is in charge of fetching and stashing the `Nuts`\n- `Serializer` dumps results to files and load them back to memory\n- `Hasher` creates unique hashes for a parameter value\n- `HashManager` coordinates hashes of all arguments and functon code\n\n## Development Setup\n\n1. Clone this repo\n2. Install [pyenv](https://github.com/pyenv/pyenv#installation).\n3. Install the python version used for development running `pyenv install` in the root of this repository.\n4. Install [poetry](https://python-poetry.org/docs/#installation). Version `1.5.1` is recommended.\n5. Run this command to make sure poetry uses the right python version `poetry env use $(which python)`\n6. Install project and dependencies with `poetry install`\n7. Run tests with `poetry run pytest` or activate the virtualenv with `poetry shell` and then run `pytest`\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Easily stash the results of expensive functions to disk",
    "version": "0.3.0",
    "project_urls": {
        "Homepage": "https://github.com/javiber/scrat",
        "Repository": "https://github.com/javiber/scrat"
    },
    "split_keywords": [
        "cache",
        "disk"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65eeb33d363bd8351ebcea6a6017065321e1a92b65284be5b2b248afe577abf7",
                "md5": "9f4694c83bb7709b92caecd1725068c8",
                "sha256": "c39395831672b63f997253295d4ac98770ff12ff7bf4a64828ae671a18046614"
            },
            "downloads": -1,
            "filename": "scrat-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9f4694c83bb7709b92caecd1725068c8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 23238,
            "upload_time": "2024-01-06T16:29:48",
            "upload_time_iso_8601": "2024-01-06T16:29:48.402986Z",
            "url": "https://files.pythonhosted.org/packages/65/ee/b33d363bd8351ebcea6a6017065321e1a92b65284be5b2b248afe577abf7/scrat-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "443ed23dc47c36891f028cfcd9995be891aba03bab32872b30fb8b54d9312434",
                "md5": "ac8a6005575ca3530107babe29ae1f67",
                "sha256": "e433d0c548fbbc585f5bd54f655571c42792572edf49c0572e362665e0c30dc4"
            },
            "downloads": -1,
            "filename": "scrat-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ac8a6005575ca3530107babe29ae1f67",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 17119,
            "upload_time": "2024-01-06T16:29:49",
            "upload_time_iso_8601": "2024-01-06T16:29:49.689308Z",
            "url": "https://files.pythonhosted.org/packages/44/3e/d23dc47c36891f028cfcd9995be891aba03bab32872b30fb8b54d9312434/scrat-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-06 16:29:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "javiber",
    "github_project": "scrat",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "scrat"
}
        
Elapsed time: 2.34760s