Skip to main content

A memoize-like decorator that caches to disk.

Project description

diskoize:memoize::disk:memory

What is this

When I use @functools.lru_cache, I often want to persist data between python process restarts. This library accomplishes that.

import diskoize
import requests

@diskoize.diskoize("scrape_cache.db")
def scrape(url):
  return requests.request(url)


scrape("google.com") # <-- will run only once, even after rerunning this script.

You need to manually delete the cache if you change the function's input-output behavior.

Tests

We use pytest for testing. To run the tests, you can use the following command:

pytest

This will run all the tests in the tests directory. You can also run a specific test file by providing the path to the file:

pytest tests/test_file.py

Development

Make a temporary pip install: pip install -e . Now you can use the library from any python script, and edits to the local diskoize/ folder will be reflected.

Remarks

  • There is a tension between making this library understandable and making it easy to use. -- we always give the option of explicitly naming the backing sqlite database file. -- for autonaming, we currently create or reuse a file in the system temp directory which is based based on the diskoized function's name. It is up to the user to delete invalidated caches. So autonaming should probably only be used for quick-and-dirty experiments where we don't expect to come back to the code after a long while.

TODOs

  • add option in_memory (default False) to store the cache in memory, always with persistent backing.
  • make the tests actually test separate processes accessing the same cache
  • add methods for interacting with the cache
  • add more examples
  • add flush() method, and a lru_cache to support it
  • fix _make_key

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

diskoize-0.1.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diskoize-0.1.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file diskoize-0.1.2.tar.gz.

File metadata

  • Download URL: diskoize-0.1.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for diskoize-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5c6b8fe64a4505098bb9cc81e0a9f01e0632d63d990d650b739948144ae6afd3
MD5 0329ec647e309b9c93f33c3fae33e18f
BLAKE2b-256 889f1a24528b734eaf5e553b88f7533fcfd247c1eb3531169d036aa555728223

See more details on using hashes here.

File details

Details for the file diskoize-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: diskoize-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for diskoize-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8633791b510c10ca28d5795e5cb5a49d8d3ed31e3cbe78d283b4676bf4014cca
MD5 7a1e8c71a31a4f6fe9943369a3955e7e
BLAKE2b-256 6fdd78155b6ed3c4beb6d222ed66894463f142b295977ab26c1aa6a76badac2c

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