typed-cache


Nametyped-cache JSON
Version 0.2.5 PyPI version JSON
download
home_pageNone
SummaryA framework for caching data locally. It features explicit caching with priority support for type hints.
upload_time2024-10-19 09:13:31
maintainerNone
docs_urlNone
authorpanhaoyu
requires_python<4.0,>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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`:
```bash
pip install typed-cache
```

### Using `poetry`:
```bash
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:

```python
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.
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "typed-cache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": "panhaoyu",
    "author_email": "panhaoyu.china@outlook.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/cc/45e4b3a21ee59caaccb90b962dcbde3f83ec39d266e8fd0c46454bb6c535/typed_cache-0.2.5.tar.gz",
    "platform": null,
    "description": "# typed-cache\n\n`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.\n\n## Features\n\n- **Type Hinting**: Enhances code clarity and safety through the use of Python's type hints.\n- **Automatic Caching**: Automatically loads cached data on initialization if the file exists.\n- **Manual Data Saving**: Allows for explicit control over when data is saved via the `save()` method.\n- **File Integrity Checks**: Ensures that cache files use the correct `.pickle` extension and manages errors properly.\n- **Python 3.6+ Compatibility**: Works with Python 3.6 and above, making it accessible for most environments.\n\n## Requirements\n\n- Python 3.6+\n- Standard libraries: `pickle`, `pathlib`\n\n## Installation\n\nTo install `typed-cache`, you can use either `pip` or `poetry`:\n\n### Using `pip`:\n```bash\npip install typed-cache\n```\n\n### Using `poetry`:\n```bash\npoetry add typed-cache\n```\n\nSimply choose your preferred package manager to install the library.\n\n## Usage\n\nHere's a simple script demonstrating how to use `typed-cache` to persist and retrieve data in a custom class:\n\n```python\nfrom dataclasses import dataclass\nfrom pathlib import Path\n\nfrom typed_cache import TypedCache\n\n\n@dataclass\nclass Settings(TypedCache):\n    volume: int = 50\n    brightness: float = 0.8\n    username: str = \"user\"\n\n\n# Specify cache file path\ncache_path = Path.home() / 'settings_cache.pickle'\n\n# Initialize settings, modify, and save\nsettings = Settings(path=cache_path)\nsettings.volume = 70\nsettings.save()  # Saves the modified settings to 'settings_cache.pickle'\n\n# On subsequent runs, the previous settings will be automatically loaded\nloaded_settings = Settings(path=cache_path)\nprint(f\"Loaded volume: {loaded_settings.volume}\")  # Output: Loaded volume: 70\n```\n\n### Key Functions:\n\n1. **Initialization**: When you instantiate your class with a cache file path, `typed-cache` will automatically attempt to load data from that file.\n2. **Saving**: Call `save()` on your object to persist the current state to the cache file.\n3. **Loading**: Upon reinitialization with the same cache path, previously saved data will be loaded automatically.\n\n## Exception Handling\n\n- **`ValueError`**: Raised if the cache file does not have a `.pickle` extension.\n- **`FileNotFoundError`**: Raised if trying to load a cache file that does not exist.\n\n## Contributing\n\nContributions are welcome! Feel free to fork the repository, make improvements, and submit a pull request.\n\n## License\n\nThis project is licensed under the MIT License.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A framework for caching data locally. It features explicit caching with priority support for type hints.",
    "version": "0.2.5",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "24a7f8d2c872c602259e11777058d926d0adf0a10489e24c235ea92fbf033424",
                "md5": "46bad6a41a5d936bcee6297ff0bdb04b",
                "sha256": "ac361e391a93ac19ef376171fbe79f4075ff69295eb187d6e82cef380631f6de"
            },
            "downloads": -1,
            "filename": "typed_cache-0.2.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "46bad6a41a5d936bcee6297ff0bdb04b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.6",
            "size": 4953,
            "upload_time": "2024-10-19T09:13:29",
            "upload_time_iso_8601": "2024-10-19T09:13:29.381758Z",
            "url": "https://files.pythonhosted.org/packages/24/a7/f8d2c872c602259e11777058d926d0adf0a10489e24c235ea92fbf033424/typed_cache-0.2.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3cc45e4b3a21ee59caaccb90b962dcbde3f83ec39d266e8fd0c46454bb6c535",
                "md5": "7878f3353c90a65bb6c89def65f95fc5",
                "sha256": "377a4b5763ccbb9517f46ba3f1b473e4913fc3de7f1b5067369421d6139a65bb"
            },
            "downloads": -1,
            "filename": "typed_cache-0.2.5.tar.gz",
            "has_sig": false,
            "md5_digest": "7878f3353c90a65bb6c89def65f95fc5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.6",
            "size": 4264,
            "upload_time": "2024-10-19T09:13:31",
            "upload_time_iso_8601": "2024-10-19T09:13:31.317744Z",
            "url": "https://files.pythonhosted.org/packages/c3/cc/45e4b3a21ee59caaccb90b962dcbde3f83ec39d266e8fd0c46454bb6c535/typed_cache-0.2.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-19 09:13:31",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "typed-cache"
}
        
Elapsed time: 0.42100s