Package: pure-func
Pure-func contains decorators that help writing pure functions in python.In python it is impossible to determine if a function is pure for certain.
Even writing a static-analysis that gets the most cases right is very hard.
Therefore pure-func checks purity at run-time in the spirit of python.The canonical way to use pure-func is:@gcd_lru_cache()@pure_check()deffib(x):ifx==0orx==1:return1returnfib(x-1)+fib(x-2)deftest_fib1(x):withchecking():returnfib(x)@checked()deftest_fib2(x):returnfib(x)# productionx=fib(30)# testingx=test_fib1(30)x=test_fib2(30)pure_checkin check-mode will run the function with its current input and
return the output, but it will also run the function against up to three past
inputs and check if the output matches to that past output. If the function is
stateful, it will probably fail that check and anNotPureExceptionis risen.Check-mode is enabled by@checked()orwith checking(), if check-mode is
not enabled, pure_check will simply pass the input and output through.If your function has discrete reoccurring input, you can usegcd_lru_cacheas
very neat way tomemoizeyour function. The cache will be cleared when python
does garbage-collection. For more long-term cache you might considerfunctools.lru_cache.IMPORTANT:@pure_check()/@pure_simpling()have always to be the
innermost (closest to the function) decorator.Writing pure functions works best when the input and output is immutable,
please consider usingpyrsistent.Memoizationwill work better with pyristent
and using multiprocessing is a lot easier withpyrsistent(no more
pickling errors).pure_samplingallows to run pure_check in production by calling the checked
function exponentially less frequent over time. Note thatpure_samplingwill
wrap the function inpure_checkso you shouldnotuse both decorators.
Also if check-mode is enabledpure_samplingwill always check the function
just likepure_check.Nice fact:with checking/@checked()will enable the check-mode for all
functions even functions that are called by other functions. So you check your
whole program, which means if functions influence each other you will probably
catch that.@pure_check()Check if the function has no side-effects during unit-tests.If check-mode is enabled using@checked()orwith checking()the
function decorated with@pure_check()will be checked for purity.First the function will be executed as normal. Then the function will be
executed against up to three (if available) past inputs in random order.
During these checks the function is guarded against recursive checks: If
the function is called recursively it will be executed as normal without
checks.If a check failsNotPureExceptionis raised.In the end result of the first (normal) execution is returned.@gcd_lru_cache(maxsize=128, typed=False)Garbage-collected least-recently-used-cache.Ifmaxsizeis set to None, the LRU features are disabled and the cache
can grow without bound.Iftypedis True, arguments of different types will be cached separately.
For example, f(3.0) and f(3) will be treated as distinct calls with
distinct results.The cache is cleared before garbage-collection is run.Arguments to the cached function must be hashable.View the cache statistics named tuple (hits, misses, maxsize, currsize)
with f.cache_info(). Clear the cache and statistics with f.cache_clear().
Access the underlying function with f.__wrapped__.See:WikipediaTypically gcd_lru_cache is good in tight loops andfunctools.lru_cacheshould be used for periodical- or IO-tasks.@pure_sampling(base=2)Check if the function has no side-effects using sampling.It allows to runpure_checkin production by calling the checked function
exponentially less over time.The distance between checks isbaseto the power ofchecksin function
calls. Assumingbase=2on third check it will be check again after 8
calls. So it will take exponentially longer after every check for the next
check to occur. It raisesNotPureExceptionif impurity has been detected.Ifbase=1the function is always checked.If check-mode is enabled the function is always checked.with checking()Enable check-mode (Context).Any functions with decorators@pure_check()or@pure_sampling()will
always be checked. Use this in unit-tests to enable checking. Nestingchecking/checkedworks fine.@checked()Enable check-mode (Decorator).Any functions with decorators@pure_check()or@pure_sampling()will
always be checked. Use this in unit-tests to enable checking. Nestingchecking/checkedworks fine.PerformancePlain fibonacci(20): 10946 (took 0.00353 seconds)
Fibonacci(20) with pure_check (direct): 10946 (took 0.01075 seconds)
Fibonacci(20) with pure_check (checked): 10946 (took 0.46707 seconds)
Fibonacci(20) with pure_sampling: 10946 (took 0.05350 seconds)
Fibonacci(20) with pure_sampling(base=1): 10946 (took 0.75588 seconds)
Fibonacci(20) with pure_sampling (checked): 10946 (took 0.83311 seconds)
Plain fibonacci(30): 1346269 (took 0.43068 seconds)
Fibonacci(30) composed (direct): 1346269 (took 0.00004 seconds)
Fibonacci(30) composed (checked): 1346269 (took 0.00001 seconds)
Fibonacci(30) with gcd_lru_cache: 1346269 (took 0.00002 seconds)
Plain expansive fibonacci(8): 34 (took 0.68938 seconds)
Expansive fibonacci(8) with pure_check: 34 (took 0.68841 seconds)
Expansive fibonacci(8) with pure_check (checked): 34 (took 9.47059 seconds)
Expansive fibonacci(8) with pure_sampling: 34 (took 1.34284 seconds)
Expansive fibonacci(8) with pure_sampling (checked): 34 (took 8.50167 seconds)
Plain mergesort (took 1.60277 seconds)
Mergesort with pure_check (direct) (took 1.60405 seconds)
Mergesort with pure_check (checked) (took 8.65274 seconds)
Mergesort with pure_sampling (took 2.45293 seconds)Note that the fibonacci function is very short, please compare to the expansive
fibonacci tests.LicenseMITChangelog1.2 - 2017-04-19Fix setup.py to point to the correct homepage (@lucaswiman)Fix @pure_sampling(base=1) not checking at all