Package: memoize-2
Introduction------------[![Build Status](https://travis-ci.org/rlgomes/memoize.png)](https://travis-ci.org/rlgomes/memoize)Using memoize-------------From wikipedia:> In computing, memoization is an optimization technique used primarily to speed> up computer programs by having function calls avoid repeating the calculation of> results for previously processed inputs.The memoize library can be used to quickly add the memoization feature to anyexisting function so that you can cache previously calculated results for thesame arguments but don't have to "clutter" your function code to add thememoization feature into your function.There is a pretty simple implementation of such a decorator in some of python'sdocumentation but the implementation itself is quite basic and won't handle afew of the use cases resolved with this simple decorator. Here is a list of thereasons why I wouldn't use the example "memoized" decorator from the pythondocumentation:1. It relies on exceptions being raised for it to decide to cache the currentresults from the function you had decorated. Relying on exceptions to bethrown to tests if something is on a cache is a bad design IMHO.2. The memoized decorator doesn't handle list types and a few others whilethe memoize decorator can be used on any type that can be converted intoa string by the str() function.3. The memoized decorator also doesn't handle the keyword arguments4. The memoize decorator allows you to customize your argument hashing functionwhich controls how you match the arguments during the caching of resultspreviously calculated. The memoized decorator doesn't have this feature.The basic memoize decorator can be used quickly by just placing the "@memoize"decorator on the line above the function definition and there is also a"memoize_with" which allows the user to define the argument to unique string idtransformation to be used when identify that the arguments being passed to yourfunction are indeed the same argument combination that was used a while ago.Lets first show a simple example of a very recursive function you may find alittle familiar:```pythondef recursive_function_normal(arg):if arg <= 0: return 1if arg == 1: return 1return recursive_function_normal(arg-1) + recursive_function_normal(arg-2)```Now this function seems innocent enough but if you have a close look at therecursive calls within this function you'll realize its quite a complex sumof the previous elements with the value of the current element. Now beforeadding the memoize decorator lets see how well this function scales:```recursive_function_normal(10) took 0msrecursive_function_normal(20) took 13msrecursive_function_normal(30) took 688msrecursive_function_normal(32) took 1605ms```That does not look promising at all if we wanted to calculate something like thevalue for 100 or 200 it would probably take a few hours. Now with memoizationapplied to this same function, like so:```python@memoizedef recursive_function_memoized(arg):if arg <= 0: return 1if arg == 1: return 1return recursive_function_memoized(arg-1) + recursive_function_memoized(arg-2)```Here are the numbers:```recursive_function_memoized(10) took 0msrecursive_function_memoized(20) took 0msrecursive_function_memoized(30) took 0msrecursive_function_memoized(32) took 0ms```Even from scratch calculation 100 is very fast:```recursive_function_memoized(100) took 2ms```You might be scratching your head thinking this is impossible but just rememberthat after you've calculated the value for 10, 20 and 30 when you issue thecall for 100 you only have a few "new" values to calculate while all the othersare pulled straight off the memoization cache.Of course there are situations where the normal memoize decorator isn't efficientenough when handling multiple arguments to your functions and when you have agood grasp on how to uniquely encode the arguments being passed to your functionin a simple string (ie hash) you can replace the built-in "hashing" mechanismwith your own custom one by using the "@memoize_with(hash_args)" declarationand then defining the hash_args with the same signature as the function you'reattempting to memoize. Here is a small example of how to write your own argumenthashing function.Installing----------```python setup.py install```or install directly from github with:```pip install -e git+git://github.com/rlgomes/memoize.git#egg=memoize```Running Built-In Tests-----------------------After installing the memoize library you can go to the tests directory and runall the tests by executing:```cd testspython all_tests.py```License-------Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0.html)