# 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
```bash
pip install sci-cache
```
## Usage
```python
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](./LICENSE) for more information.
Raw data
{
"_id": null,
"home_page": "https://github.com/panhaoyu/sci-cache",
"name": "sci-cache",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "cache, scientific, data, performance, optimization",
"author": "panhaoyu",
"author_email": "panhaoyu.china@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/ff/2b/2ff5a933b9005c5954ca500db5a563e5190bc50e5bc931228f44c5744d5d/sci_cache-0.1.2.tar.gz",
"platform": null,
"description": "# sci-cache\n\nA Python library for caching scientific data on disk, streamlining research workflows by storing intermediate results.\n\n## Features\n\n- **Method-Based Caching**: Automatically cache each method's result based on the method name to the specified directory.\n- **Easy Integration**: Inherit from `MethodDiskCache` and use the `@method_cache` decorator to enable caching.\n- **Flexible Storage**: Store cached data in a designated folder, organized and managed automatically.\n- **Supports Various Data Types**: Ideal for scientific data processing tasks, handling complex data types efficiently.\n\n## Installation\n\n```bash\npip install sci-cache\n```\n\n## Usage\n\n```python\nfrom pathlib import Path\n\nfrom sci_cache import MethodDiskCache, method_cache\n\n\n# Define a caching class by inheriting from MethodDiskCache\nclass ScientificCache(MethodDiskCache):\n compute_square_count = 0 # Track compute_square calls\n compute_cube_count = 0 # Track compute_cube calls\n\n def get_cache_folder(self) -> Path:\n # Set the cache directory to 'cache' folder in the current script's directory\n return Path(__file__).parent / \"cache\"\n\n @method_cache\n def compute_square(self) -> int:\n # Increment the count to track actual computation\n ScientificCache.compute_square_count += 1\n return 3 * 3 # Example computation\n\n @method_cache\n def compute_cube(self) -> int:\n # Increment the count to track actual computation\n ScientificCache.compute_cube_count += 1\n return 2 * 2 * 2 # Example computation\n\n\n# Instantiate the caching class and use the cached methods\ncache1 = ScientificCache()\n\n# First call: performs computation and caches the result\nsquare1 = cache1.compute_square()\nassert square1 == 9\nassert ScientificCache.compute_square_count == 1\n\n# Second call: retrieves the result from cache without recomputing\nsquare2 = cache1.compute_square()\nassert square2 == 9\nassert ScientificCache.compute_square_count == 1\n\n# First call: performs computation and caches the result\ncube1 = cache1.compute_cube()\nassert cube1 == 8\nassert ScientificCache.compute_cube_count == 1\n\n# Second call: retrieves the result from cache without recomputing\ncube2 = cache1.compute_cube()\nassert cube2 == 8\nassert ScientificCache.compute_cube_count == 1\n\n# Re-instantiate the caching class to simulate a new session\ncache2 = ScientificCache()\n\n# Call methods again to ensure cached results are used\nsquare3 = cache2.compute_square()\nassert square3 == 9\nassert ScientificCache.compute_square_count == 1\n\ncube3 = cache2.compute_cube()\nassert cube3 == 8\nassert ScientificCache.compute_cube_count == 1\n\nprint(\"All assertions passed.\")\n```\n\n## License\n\nMIT License. See [LICENSE](./LICENSE) for more information.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python library for caching scientific data on disk, streamlining research workflows by storing intermediate results.",
"version": "0.1.2",
"project_urls": {
"Documentation": "https://github.com/panhaoyu/sci-cache",
"Homepage": "https://github.com/panhaoyu/sci-cache",
"Repository": "https://github.com/panhaoyu/sci-cache"
},
"split_keywords": [
"cache",
" scientific",
" data",
" performance",
" optimization"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d984a1adf652ad53737f020b0a480311eaf90083cbca78451cbdf8f175db682a",
"md5": "29df6ffc854d853c6cd9245495aa49a0",
"sha256": "dbee9bf237dfa7baa6da42b94ec9468901f0e845aa35661e34092c5fa3e9980c"
},
"downloads": -1,
"filename": "sci_cache-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "29df6ffc854d853c6cd9245495aa49a0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 7680,
"upload_time": "2024-12-31T04:07:00",
"upload_time_iso_8601": "2024-12-31T04:07:00.141232Z",
"url": "https://files.pythonhosted.org/packages/d9/84/a1adf652ad53737f020b0a480311eaf90083cbca78451cbdf8f175db682a/sci_cache-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ff2b2ff5a933b9005c5954ca500db5a563e5190bc50e5bc931228f44c5744d5d",
"md5": "d3e21c3e65f425a7838c47a5161ae2f4",
"sha256": "83c16e01f7ffe3e282354dae29c03244b36eca5c5243fb93bb5ba5eeb3627a7a"
},
"downloads": -1,
"filename": "sci_cache-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "d3e21c3e65f425a7838c47a5161ae2f4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 6194,
"upload_time": "2024-12-31T04:07:01",
"upload_time_iso_8601": "2024-12-31T04:07:01.387528Z",
"url": "https://files.pythonhosted.org/packages/ff/2b/2ff5a933b9005c5954ca500db5a563e5190bc50e5bc931228f44c5744d5d/sci_cache-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-31 04:07:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "panhaoyu",
"github_project": "sci-cache",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "sci-cache"
}