Skip to main content

Predicative dispatch decorator for Python, based on idea from book "Functional Programming in Python".

Project description

Predicate_dispatch

Github Actions Coverage Status Code style: black pdm-managed

Predicative dispatch decorator for Python, based on the idea from Functional 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 @predicate decorator must be equal to the number of arguments of the wrapped function.

Installation

Predicate_dispatch requires Python 3.7 or higher.

pip install predicate_dispatch

Basic usage

from predicate_dispatch import predicate

@predicate(lambda x: x > 1)
def factorial(x):
  return x * factorial(x - 1)

@predicate()
def factorial(x):
  return x
        
factorial(5) == 120

@predicate() - is the default predicate. It is used when none of the other predicates resolves to True.

Caching

Types of cache in predicate_dispatch functions

Decorator name Cache type
predicate Without cache
predicate_cache Function choice is cached
predicate_cache_result Function result is cached

predicate - without cache

You must use the predicate decorator if the result of the lambda function in the predicate for the same argument x changes over time. For example, if you compare x to the current time or execution count.

If you don't want to dig deeper into the quirks of caching just use the predicate decorator.

For example:

def get_events(x):
    if x >= time.time():
        return database.getFutureEvents()
    if x < time.time():
        return database.getPastEvents()
    return []

To this:

@predicate(lambda x: x >= time.time())
def get_events(x):
    return database.getFutureEvents()

@predicate(lambda x: x < time.time())
def get_events(x):
    return database.getPastEvents()

@predicate()
def get_events(x):
    return []

predicate_cache - function choice is cached

You may use the predicate_cache decorator if the result of the lambda function in the predicate for the same argument x does not change over time, but the result from calling the real function will change. This decorator will cache what function was called previously for certain x. For example, if you have code like this:

def get_logs(x):
    if x == 1:
        return database.getLogsForDog()
    if x == 2:
        return database.getLogsForCat()
    if x == 3:
        return database.getLogsForFox()
    return database.getLogsForOtherAnimal()

You may rewrite it to the predicate_cache decorator like this:

@predicate_cache(lambda x: x == 1)
def get_logs(x):
    return database.getLogsForDog()

@predicate_cache(lambda x: x == 2)
def get_logs(x):
    return database.getLogsForCat()

@predicate_cache(lambda x: x == 3)
def get_logs(x):
    return database.getLogsForFox()

@predicate_cache()
def get_logs(x):
    return database.getLogsForOtherAnimal()

When you first call get_logs(3) internally it will compare 3 then to 1, then 3 to 2, then 3 to 3 and execute db.getLogsForFox(). When you call get_logs(3) again it will take the previous function choice from the cache and straightaway execute db.getLogsForFox().

predicate_cache_result - function result is cached

You may use the predicate_cache_result decorator if the result of the lambda function in the predicate for the same argument x does 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 certain x. A typical example would be when you want to use predicate_dispatch instead of many static ifs.

From this:

def get_animal(x):
    if x == 1:
        return "dog"
    if x == 2:
        return "cat"
    if x == 3:
        return "fox"
    return "animal"

To this:

@predicate_cache_result(lambda x: x == 1)
def get_animal(x):
    return "dog"

@predicate_cache_result(lambda x: x == 2)
def get_animal(x):
    return "cat"

@predicate_cache_result(lambda x: x == 3)
def get_animal(x):
    return "fox"

@predicate_cache_result()
def get_animal(x):
    return "animal"

When you first call get_animal(3) internally it will compare 3 then to 1, then 3 to 2, then 3 to 3 and then return result "fox". When you call get_animal(3) again it will take the previous result from the cache and return it.

Limitations

  • The number of arguments in the condition function in the @predicate or @predicate_cache or @predicate_cache_result decorator 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.

License

Copyright 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 projects

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

predicate-dispatch-0.4.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

predicate_dispatch-0.4.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file predicate-dispatch-0.4.0.tar.gz.

File metadata

  • Download URL: predicate-dispatch-0.4.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for predicate-dispatch-0.4.0.tar.gz
Algorithm Hash digest
SHA256 fcb4c133124d5c0390d0026d4604be6a895323ef71e078e88efc913d74e0748d
MD5 dcecc19bdc703b07320c8b21302a688a
BLAKE2b-256 c871c8cd2792495a57fc6f4e4c5339aee65d9598b4afc6935b0c33cca9363715

See more details on using hashes here.

File details

Details for the file predicate_dispatch-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for predicate_dispatch-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bbd8f1b07a43c2d139f553a294a862c798ebd01bf4ecc07cc2540088a05a7c1
MD5 3e0ddf19a6d9826aafb4369066f5953c
BLAKE2b-256 5c202ae269057a75d8a9f6ed7e539622a8469ffc870bcd2a6c648a64288567d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page