Package: memopy
Store results of functions calls so that subsequent calls with the same arguments do not have to redo work.This strategy of memoizing past results is only fully correct with “pure functions”. Pure functions (https://en.wikipedia.org/wiki/Pure_function) are functions that, when run with the same arguments, always return the same result. They should not have any side effects (e.g. modifying global variables, writing to an external database), or depend on anything besides the parameters (e.g. a random number generator, reading from an external database). Typically any mathematically-defined function (fibonacci, factorial, sine, cosine, etc.) is a pure function.But MemoPy can be useful with impure functions, too, when perfect correctness is not required. For example, it can be used to cache the results of network calls to external API’s, to avoid repeating expensive network requests (but the cache should be cleared when it is necessary to get completely up to date information).UseImport the memoify decorator:from memopy.memopy import memoifyApply thememoifydecorator to a function to turn that function into a memo function:@memoify
def multiply(x, y):
    return x * ySubsequent calls tomultiplywith the same args will not perform the multiplication, but rather look up the past answer.Multiplying numbers is not a typical use case, so take a look at a function which is inherently reused on the same arguments a lot:@memoify
def fibo(n):
    if n in [0, 1]:
        return n
    return fibo(n-1) + fibo(n-2)Without memoization, some values of the fibonacci sequence would be computed an exponential number of times. With the single line@memoify, for a givennevery computation after the first will be replaced with a lookup. Check out the difference between the above memoified version of fibo and the regular version of that same function:def regular_fibo(n):
    if n in [0, 1]:
        return n
    return regular_fibo(n-1) + regular_fibo(n-2)See how the difference grows asngrows larger. (For me, for example, withn = 30, the difference is already very noticable, and withn = 100,regular_fibotakes forever while the memoizedfibois still instant.)HashabilityFunction arguments are notrequiredto be hashable, but they should be for best (fastest) results. If they are not, the memoizing version of the function could become slower than the original under special circumstances (depending on the runtime of the original function, and the number and nature of different arguments the memoizing version has been called with).ConcurrencyMemoPy was not designed with concurrency in mind. So multiple runs of the same function in different threads at the same time is advised against as it has not been thoroughly thought through what would happen.DocumentationThe tests found on Github athttps://github.com/tscizzle/memopy/tree/master/testsgive some examples and showcase the library’s functionality.InstallationIf you don’t have pip, get pip at:https://pip.pypa.io/en/stable/installingRun the commandpip install memopyin your terminal to get the MemoPy library.To test your installation, start a Python interpreter with thepythoncommand in your terminal and make sure you can runimport memopyin it without getting an error.ContributeFind the code on Github at:https://github.com/tscizzle/memopySupportContact me (Tyler Singer-Clark) attscizzle@gmail.comwith any questions or concerns.LicenseThe project is licensed under the MIT license.