Skip to main content

Parse Markdown form and generate useful templates and data.

Project description

Latest Version License Python Versions CI LINTER Coverage

flexcache

An robust and extensible package to cache on disk the result of expensive calculations.

Consider an expensive function parse that takes a path and returns a parsed version:

>>> content = parse("source.txt")

It would be nice to automatically and persistently cache this result and this is where flexcache comes in.

First, we create a DiskCache object:

>>> from flexcache import DiskCacheByMTime
>>> dc = DiskCacheByMTime(cache_folder="/my/cache/folder")

and then is loaded:

>>> content = dc.load("source.txt", reader=parse)

If this is the first call, as the cached result is not available, parse will be called on source.txt and the output will be saved and returned. The next time, the cached will be loaded and returned.

When the source is changed, the DiskCache detects that the cached file is older, calls parse again storing and returning the new result.

In certain cases you would rather detect that the file has changed by hashing the file. Simply use DiskCacheByHash instead of DiskCacheByMTime.

Cached files are saved using the pickle protocol, and each has a companion json file with the header content.

Building your own caching logic

In certain cases you would like to customize how caching and invalidation is done. You can achieve this by subclassing the DiskCache.

>>> from flexcache import DiskCache
>>> class MyDiskCache(DiskCache):
...
...    @dataclass(frozen=True)
...    class MyHeader(NameByPathHeader, BasicPythonHeader):
...         pass
...
...    _header_classes = {pathlib.Path: MyHeader}

Here we create a custom Header class and use it to handle pathlib.Path objects. We provide a convenient set of Header classes.

You can also avoid saving the header by setting the _store_header class attribute to False.

MinimumHeader

  • source object limitations: None

  • header content: source object and identifier of the reader function.

  • values used for naming: identifier of the reader function.

  • invalidating logic: the cached file exists.

BasicPythonHeader: same as MinimumHeader but …

  • source object limitations: None

  • header content: source and identifier of the reader function, platform, python implementation, python version.

  • values used for naming: identifier of the reader function, platform, python implementation, python version.

  • invalidating logic: the cached file exists.

NameByFileContentHeader: must be subclassed with MinimumHeader or BasicPythonHeader

  • source object limitations: must be a pathlib.Path

  • header content: depends on the sibling classes.

  • values used for naming: adds file content as bytes.

  • invalidating logic: the cached file exists.

NameByObjHeader: must be subclassed with MinimumHeader or BasicPythonHeader

  • The source object must be pickable.

  • header content: depends on the sibling classes. Adds pickle_protocol.

  • values used for naming: adds pickled object using pickle_protocol version.

  • invalidating logic: the cached file exists.

NameByPathHeader: must be subclassed with MinimumHeader or BasicPythonHeader

  • source object limitations: must be a pathlib.Path

  • header content: depends on the sibling classes.

  • values used for naming: adds resolved path.

  • invalidating logic: the cached file exists and is newer than the source.

NameByPathHeader: must be subclassed with MinimumHeader or BasicPythonHeader

  • source object limitations: must be a pathlib.Path

  • header content: depends on the sibling classes.

  • values used for naming: adds resolved paths.

  • invalidating logic: the cached file exists and is newer than the newest source.

but you can make your own. Take a look at the code!


This project was started as a part of Pint, the python units package.

See AUTHORS for a list of the maintainers.

To review an ordered list of notable changes for each version of a project, see CHANGES

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

flexcache-0.1.tar.gz (12.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page