Skip to main content

A utility package for memoizing functions and class methods

Project description

Introduction

Memoization is a common technique used primarily as an optimizer, allowing the caching of the return values of expensive functions. When a memoized function is called with a given set of arguments for the first time its return value is cached, and that value is then returned on all subsequent calls.

While many memoization packages exist, as well as instructions to write your own simple decorator, this package provides enhanced utility for memoizing methods on classes. Especially, this allows for a couple of ways of automatically clearing the caches related to a particular instance, for example, when changing a member variable would change the result of those functions. This package also uses inspect.getcallargs to correctly treat default argument values, where possible.

Preliminary sphinx documentation can be found at https://jon-burr.github.io/memoclass/memoclass.html

The project is hosted on github at https://github.com/Jon-Burr/memoclass

Installation

pip install memoclass

Overview of core components

memoclass.memoize.memofunc

Memoizes a single free function. The returned object is a MemoFunc object, which has functions that allow you to temporarily disable the caching, or clear it entirely.

>>> from memoclass.memoize import memofunc
>>>
>>> @memofunc
>>> def build_list(x):
>>>     return [1, 2, 3, x]
>>>
>>> a = build_list(5)
>>> b = build_list(5)
>>> a is b
True
>>> build_list.clear_cache()
>>> c = build_list(5)
>>> a is c
False

The MemoFunc class also has some extra properties that can be set while decorating

>>> from memoclass.memoize import memofunc
>>> import copy
>>>
>>> @memofunc(on_return=copy.copy)
>>> def build_list(x):
>>>     return [1, 2, 3, x]
>>>
>>> a = build_list(5)
>>> b = build_list(5)
>>> a is b
False

memoclass.memoize.memomethod

Memoizes a class method. Methods bound to different instances have independent caches, so the cache on one object can be cleared without clearing it for all other objects.

>>> from memoclass.memoize import memomethod
>>>
>>> class ListBuilder(object):
>>>     @memomethod
>>>     def __call__(self, x):
>>>         return [1, 2, 3, x]
>>>
>>> x = ListBuilder()
>>> y = ListBuilder()
>>> a = x()
>>> b = y()
>>> a is b
False
>>> x.__call__.clear_cache()
>>> c = y()
>>> b is c # Clearing x's cache has not touched y's
True

memoclass.memoclass.MemoClass

Base class meant to make interacting with memoized methods easier. It can enabled, disable and clear all memomethods attached to an instance (note that which methods exist is calculated at the class level, so any added onto an instance will not be seen).

By default, setting any attribute will reset the object’s caches, unless that attribute has been provided to the mutable_attrs argument of MemoClass.__init__. This behaviour can be disabled by setting mutable_attrs=None. Additionally, any function can have the memoclass.memoclass.mutates decorator applied to it, which will then reset the caches whenever it is called.

>>> from memoclass.memoize import memomethod
>>> from memoclass.memoclass import MemoClass
>>>
>>> class PartialSum(MemoClass):
>>>
>>>     def __init__(self, stored):
>>>         super().__init__()
>>>         self.stored = stored
>>>
>>>     @memomethod
>>>     def __call__(self, other):
>>>         return self.stored + other
>>>
>>> a = PartialSum(5)
>>> a(3)
8
>>> a.stored = 3 # Triggers a cache reset
>>> a(3)
6

A MemoClass can be locked which means that all caches are enabled and calling a function marked mutates or setting a non-mutable attribute results in a ValueError. When the class is then unlocked again, if the caches were previously disabled, they will be disabled and cleared. This means it is possible to create a class whose methods are only temporarily memoized. This might be useful if a class has expensive methods to calculate that rely on a global state. Note that by default, a memomethod declared on a MemoClass will lock its caller while it is called.

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

memoclass-0.1.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

memoclass-0.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file memoclass-0.1.0.tar.gz.

File metadata

  • Download URL: memoclass-0.1.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

File hashes

Hashes for memoclass-0.1.0.tar.gz
Algorithm Hash digest
SHA256 383eb37edd31222c4c32d13351d440d008c0cef796e51b4b7d16005510419821
MD5 311aa646f81f0505b02fdf0750582ddd
BLAKE2b-256 7e9f0cfa252e7598abc8623fb12dcc875243352399c2dd07c89404b3815d4a09

See more details on using hashes here.

File details

Details for the file memoclass-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: memoclass-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

File hashes

Hashes for memoclass-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d1c31415c341691330ef3b6cbc843b4d6861e319f01e9bf8c9af12a535e3bf0
MD5 a3b8a93e28f8f3c9ad7abe26f0611caa
BLAKE2b-256 50d44fc3f7cbf303552a0873dadd0d38158dbc8c00c97d03412de9fa8976aee9

See more details on using hashes here.

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