Skip to main content

No project description provided

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 @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, 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"

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

    @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.1.tar.gz (6.0 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.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sci_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 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.1.tar.gz
Algorithm Hash digest
SHA256 909447c4a49259f6ed08868b4e541c0e5b4a87c1579937801742dc326116129f
MD5 e6b0fcad8532fe7240bd95ed0119e1e0
BLAKE2b-256 1654cd47c4164cf361939f689260390858d2560eae5040a66c46bc5f7375c53d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sci_cache-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b83ddbb657b7804f4d9d36dc066880aaf144c9c6279fe007d3d75120bbcce3ce
MD5 251f4b327320beb8bf6eeee81f111a34
BLAKE2b-256 238e82a56c0e1ee51eed22cf54e30c649d533e6a1c5a66c9edde24a73be356b8

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