Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sci_cache-0.1.3.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sci_cache-0.1.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file sci_cache-0.1.3.tar.gz.

File metadata

  • Download URL: sci_cache-0.1.3.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.11 Windows/10

File hashes

Hashes for sci_cache-0.1.3.tar.gz
Algorithm Hash digest
SHA256 961285053580f481e1de4ef4c30ed870a442693f1432d9c56e2c0b9f4b21fc39
MD5 2beccaa3f656b58c32a993f578fb2912
BLAKE2b-256 90d37bd334f967b071f480c3aa5c0484836f4bd33fd82f2603633002e2b1ea52

See more details on using hashes here.

File details

Details for the file sci_cache-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: sci_cache-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.11 Windows/10

File hashes

Hashes for sci_cache-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1fac22b1fb0fdd22b248acbe1181fffcc07ebeb96d4bd0ba90897851f4a10527
MD5 45f41111f0bd6a709e50db3214e93d60
BLAKE2b-256 868165a72ebe9bf4d91493d137242f613d8f821dea8dec36eb116e294977b265

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page