Skip to main content

sci-cache

A Python library for caching scientific data on disk, streamlining research workflows by storing intermediate results.

Features

  • Method-Based Caching: Automatically cache each method's result based on the method name to the specified directory.
  • Easy Integration: Inherit from MethodDiskCache and use the @sci_method_cache decorator to enable caching.
  • Flexible Storage: Store cached data in a designated folder, organized and managed automatically.
  • Supports Various Data Types: Ideal for scientific data processing tasks, handling complex data types efficiently.

Installation

pip install sci-cache

Usage

from pathlib import Path

from sci_cache import MethodDiskCache, sci_method_cache


# Define a caching class by inheriting from MethodDiskCache
class ScientificCache(MethodDiskCache):
    compute_square_count = 0  # Track compute_square calls
    compute_cube_count = 0  # Track compute_cube calls

    def get_cache_folder(self) -> Path:
        # Set the cache directory to 'cache' folder in the current script's directory
        return Path(__file__).parent / "cache"

    @sci_method_cache
    def compute_square(self) -> int:
        # Increment the count to track actual computation
        ScientificCache.compute_square_count += 1
        return 3 * 3  # Example computation

    @sci_method_cache
    def compute_cube(self) -> int:
        # Increment the count to track actual computation
        ScientificCache.compute_cube_count += 1
        return 2 * 2 * 2  # Example computation


# Instantiate the caching class and use the cached methods
cache1 = ScientificCache()

# First call: performs computation and caches the result
square1 = cache1.compute_square()
assert square1 == 9
assert ScientificCache.compute_square_count == 1

# Second call: retrieves the result from cache without recomputing
square2 = cache1.compute_square()
assert square2 == 9
assert ScientificCache.compute_square_count == 1

# First call: performs computation and caches the result
cube1 = cache1.compute_cube()
assert cube1 == 8
assert ScientificCache.compute_cube_count == 1

# Second call: retrieves the result from cache without recomputing
cube2 = cache1.compute_cube()
assert cube2 == 8
assert ScientificCache.compute_cube_count == 1

# Re-instantiate the caching class to simulate a new session
cache2 = ScientificCache()

# Call methods again to ensure cached results are used
square3 = cache2.compute_square()
assert square3 == 9
assert ScientificCache.compute_square_count == 1

cube3 = cache2.compute_cube()
assert cube3 == 8
assert ScientificCache.compute_cube_count == 1

print("All assertions passed.")

License

MIT License. See LICENSE for more information.

Metadata

Release files for sci-cache 0.1.4

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

Source distribution (sdist)

Source distribution for sci-cache 0.1.4
File Size Uploaded Provenance
sci_cache-0.1.4.tar.gz 6.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sci-cache 0.1.4
File Interpreter ABI Platform
sci_cache-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / sci_cache-0.1.4.tar.gz

Download URL sci_cache-0.1.4.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d46538a101b014be31239685b379c0ce64cfc997708c33276921acb5d6d8e836
BLAKE2b-256 checksum
How to use checksums
73df867a9aa9bd8291b3f804ac4aa3463af2512c798a7a63094c69688a8b5dd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.11 Windows/10

Release files / sci_cache-0.1.4-py3-none-any.whl

Download URL sci_cache-0.1.4-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54fa84f27e8fca515d6765948cd0c1a019c44249a2efe78214a4c36aa34fbef7
BLAKE2b-256 checksum
How to use checksums
22befe1413757ec95b2a7fe5b2404dce43e9f074cb259fb42d757ddfb2b25d13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.11 Windows/10

Security for sci-cache 0.1.4

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