Skip to main content

typed-cache

typed-cache is a lightweight framework designed for caching data locally, supporting explicit caching with a focus on Python's type hints. It simplifies the process of persisting data to disk by allowing easy integration into custom data classes, with automatic loading and manual saving of data.

Features

  • Type Hinting: Enhances code clarity and safety through the use of Python's type hints.
  • Automatic Caching: Automatically loads cached data on initialization if the file exists.
  • Manual Data Saving: Allows for explicit control over when data is saved via the save() method.
  • File Integrity Checks: Ensures that cache files use the correct .pickle extension and manages errors properly.
  • Python 3.6+ Compatibility: Works with Python 3.6 and above, making it accessible for most environments.

Requirements

  • Python 3.6+
  • Standard libraries: pickle, pathlib

Installation

To install typed-cache, you can use either pip or poetry:

Using pip:

pip install typed-cache

Using poetry:

poetry add typed-cache

Simply choose your preferred package manager to install the library.

Usage

Here's a simple script demonstrating how to use typed-cache to persist and retrieve data in a custom class:

from dataclasses import dataclass
from pathlib import Path

from typed_cache import TypedCache


@dataclass
class Settings(TypedCache):
    volume: int = 50
    brightness: float = 0.8
    username: str = "user"


# Specify cache file path
cache_path = Path.home() / 'settings_cache.pickle'

# Initialize settings, modify, and save
settings = Settings(path=cache_path)
settings.volume = 70
settings.save()  # Saves the modified settings to 'settings_cache.pickle'

# On subsequent runs, the previous settings will be automatically loaded
loaded_settings = Settings(path=cache_path)
print(f"Loaded volume: {loaded_settings.volume}")  # Output: Loaded volume: 70

Key Functions:

  1. Initialization: When you instantiate your class with a cache file path, typed-cache will automatically attempt to load data from that file.
  2. Saving: Call save() on your object to persist the current state to the cache file.
  3. Loading: Upon reinitialization with the same cache path, previously saved data will be loaded automatically.

Exception Handling

  • ValueError: Raised if the cache file does not have a .pickle extension.
  • FileNotFoundError: Raised if trying to load a cache file that does not exist.

Contributing

Contributions are welcome! Feel free to fork the repository, make improvements, and submit a pull request.

License

This project is licensed under the MIT License.

Metadata

Release files for typed-cache 0.2.5

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

Source distribution (sdist)

Source distribution for typed-cache 0.2.5
File Size Uploaded Provenance Actions
typed_cache-0.2.5.tar.gz 4.3 kB Without provenance Details for typed_cache-0.2.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typed-cache 0.2.5
File Interpreter ABI Platform Actions
typed_cache-0.2.5-py3-none-any.whl Python 3 none any Details for typed_cache-0.2.5-py3-none-any.whl

Total release size: 9.2 kB

Release files / typed_cache-0.2.5.tar.gz

Download URL typed_cache-0.2.5.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
377a4b5763ccbb9517f46ba3f1b473e4913fc3de7f1b5067369421d6139a65bb
BLAKE2b-256 checksum
How to use checksums
c3cc45e4b3a21ee59caaccb90b962dcbde3f83ec39d266e8fd0c46454bb6c535
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 / typed_cache-0.2.5-py3-none-any.whl

Download URL typed_cache-0.2.5-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac361e391a93ac19ef376171fbe79f4075ff69295eb187d6e82cef380631f6de
BLAKE2b-256 checksum
How to use checksums
24a7f8d2c872c602259e11777058d926d0adf0a10489e24c235ea92fbf033424
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 typed-cache 0.2.5

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