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.4.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.4-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sci_cache-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 d46538a101b014be31239685b379c0ce64cfc997708c33276921acb5d6d8e836
MD5 f52eff09d1720052d7cbaa5416b09b3e
BLAKE2b-256 73df867a9aa9bd8291b3f804ac4aa3463af2512c798a7a63094c69688a8b5dd0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sci_cache-0.1.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 54fa84f27e8fca515d6765948cd0c1a019c44249a2efe78214a4c36aa34fbef7
MD5 4d16c9bf391c3764b42a1febbc110123
BLAKE2b-256 22befe1413757ec95b2a7fe5b2404dce43e9f074cb259fb42d757ddfb2b25d13

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