Git Product home page Git Product logo

locache's Introduction

locache - a local cache for Python

PyPI PyPI - Downloads GitHub codecov

A single-file utility library for caching the results of deterministic and pure function calls to disk, exposed via the @persist decorator.

These caches are persistent across runs of the program. If changes to the function's source code are detected, the cache is reset. This can also be manually performed with the reset function.

Installation

locache is simple enough that you could have written it yourself. To use it in your project, either:

  • copy the locache.py file into your project
  • install it with pip install locache

Usage

locache provides 3 importable functions:

  • the @persist decorator
  • reset
  • verbose

@persist

Wrap a pure function with @persist to cache its results to disk. The only stipulation is that the function's arguments and return value must be pickle-able.

from locache import persist

@persist
def my_func(x, num=3):
    print("Hi from foo!")
    return x * num

my_func(1)        
# prints "Hi from foo!", returns 3
my_func(2, num=2) 
# prints "Hi from foo!", returns 4
my_func(1)        
# returns 3

reset(func: Callable)

Reset the cache for a given function.

from locache import reset

reset(my_func)
my_func(1) # prints "Hi from foo!", returns 3

verbose(yes: bool = True)

Turn on verbose logging.

from locache import verbose

reset(my_func)
verbose(yes=True)

my_func(1) 
# prints "cache miss for squared(1)"
# prints "Hi from foo!"
# returns 3

my_func(1)
# prints "cache hit for squared(1)"
# returns 3

locache's People

Contributors

jla-gardner avatar

Stargazers

Congcong Sun avatar Zoé Faure Beaulieu avatar Daniel Thomas du Toit avatar Joe Morrow avatar  avatar

Watchers

 avatar

locache's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.