Package: predicate-dispatch
Predicate_dispatchPredicative dispatch decorator for Python, based on the idea fromFunctional Programming in Python.The module is providing means to specify conditions with a lambda function that determines which function is called.
The number of arguments in the condition function in the@predicatedecorator must be equal to the number of arguments of the wrapped function.InstallationPredicate_dispatch requires Python 3.7 or higher.pip install predicate_dispatchBasic usagefrompredicate_dispatchimportpredicate@predicate(lambdax:x>1)deffactorial(x):returnx*factorial(x-1)@predicate()deffactorial(x):returnxfactorial(5)==120@predicate()- is the default predicate. It is used when none of the other predicates resolves to True.CachingTypes of cache in predicate_dispatch functionsDecorator nameCache typepredicateWithout cachepredicate_cacheFunction choice is cachedpredicate_cache_resultFunction result is cachedpredicate- without cacheYou must use thepredicatedecorator if the result of the lambda function in the predicate for the same argumentxchanges over time. For example, if you comparexto the current time or execution count.If you don't want to dig deeper into the quirks of caching just use thepredicatedecorator.For example:defget_events(x):ifx>=time.time():returndatabase.getFutureEvents()ifx<time.time():returndatabase.getPastEvents()return[]To this:@predicate(lambdax:x>=time.time())defget_events(x):returndatabase.getFutureEvents()@predicate(lambdax:x<time.time())defget_events(x):returndatabase.getPastEvents()@predicate()defget_events(x):return[]predicate_cache- function choice is cachedYou may use thepredicate_cachedecorator if the result of the lambda function in the predicate for the same argumentxdoes not change over time, but the result from calling the real function will change. This decorator will cache what function was called previously for certainx. For example, if you have code like this:defget_logs(x):ifx==1:returndatabase.getLogsForDog()ifx==2:returndatabase.getLogsForCat()ifx==3:returndatabase.getLogsForFox()returndatabase.getLogsForOtherAnimal()You may rewrite it to thepredicate_cachedecorator like this:@predicate_cache(lambdax:x==1)defget_logs(x):returndatabase.getLogsForDog()@predicate_cache(lambdax:x==2)defget_logs(x):returndatabase.getLogsForCat()@predicate_cache(lambdax:x==3)defget_logs(x):returndatabase.getLogsForFox()@predicate_cache()defget_logs(x):returndatabase.getLogsForOtherAnimal()When you first callget_logs(3)internally it will compare3then to1, then3to2, then3to3and executedb.getLogsForFox(). When you callget_logs(3)again it will take the previous function choice from the cache and straightaway executedb.getLogsForFox().predicate_cache_result- function result is cachedYou may use thepredicate_cache_resultdecorator if the result of the lambda function in the predicate for the same argumentxdoes not change over time and the result from calling the real function does not change over time. This decorator will cache the previous result of calling the function with a certainx. A typical example would be when you want to usepredicate_dispatchinstead of many static ifs.From this:defget_animal(x):ifx==1:return"dog"ifx==2:return"cat"ifx==3:return"fox"return"animal"To this:@predicate_cache_result(lambdax:x==1)defget_animal(x):return"dog"@predicate_cache_result(lambdax:x==2)defget_animal(x):return"cat"@predicate_cache_result(lambdax:x==3)defget_animal(x):return"fox"@predicate_cache_result()defget_animal(x):return"animal"When you first callget_animal(3)internally it will compare3then to1, then3to2, then3to3and then return result"fox". When you callget_animal(3)again it will take the previous result from the cache and return it.LimitationsThe number of arguments in the condition function in the@predicateor@predicate_cacheor@predicate_cache_resultdecorator must be equal to the number of arguments of the wrapped function.Only one function as an argument in the predicate.Only the last default predicate will be executed.LicenseCopyright 2015 Juraj Sebin <sebin.juraj@gmail.com>
Copyright 2022 Dmitriy Pertsev <davaeron@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.Similar projectsplummultipledispatch