Skip to content

antolonappan/pycachera

Repository files navigation

PyCachera

A powerful python decorator to cache functions

Tests PyPI / GitHub CodeQL Docker wakatime Documentation Status

Installation

In command line:

pip install pycachera

Usage

Import the pycachera as:

from pycachera import cache

For caching a function

@cache()
def sum(a,b):
   return a+b

For caching a class attributes.

class Math:
   def __init__(self):
       pass
   
   @cache()
   def sum(a,b):
      return a + b  

For caching to a specific directory.

@cache(cachefolder='/home/user/scratch')
def sum(a,b):
   return a+b

For caching with a specific key

@cache(cachekey='This a custom cache')
def sum(a,b):
   return a+b

For printing the info

@cache(verbose=True)
def sum(a,b):
   return a+b

While debugging your script if you want to ignore the cached values you can recache

@cache(recache=True)
def sum(a,b):
   return a+b

When you use cache with a class attribute, and if you want to consider some attributes of that class for caching,

class Math:
   def __init__(self,c):
       self.c = c
   
   @cache(extrarg=['c'])
   def sum(a,b):
      return a + b  

To cache a class property,

class math:
    def __init__(self,a,b):
        self.a = a
        self.b = b
    
    @property
    @cache(extrarg=['a','b'],verbose=True)
    def add(self):
        return self.a + self.b

When using mpi4py.MPI,

@cache(with_mpi=True)
def sum(a,b):
   return a+b

API

visit pycachera.readthedocs.io