Calling Different Functions in Python Based on Values in a List

2024/10/9 10:25:06

I have a script that takes a list of metrics as an input, and then fetches those metrics from the database to perform various operations with them.

My problem is that different clients get different subsets of the metrics, but I don't want to write a new IF block every time we add a new client. So right now, I have a large IF block that calls different functions based on whether the corresponding metric is in the list. What is the most elegant or Pythonic way of handling this?

Setup and function definitions:

clientOne = ['churn','penetration','bounce']
clientTwo = ['engagement','bounce']def calcChurn(clientId):churn = cursor.execute(sql to get churn)[...]return churndef calcEngagement(clientId):engagement = cursor.execute(sql to get engagement)[...]return engagement

Imagine three other functions in a similar format, so there is one function that corresponds to each unique metric. Now here is the block of code in the script that takes the list of metrics:

def scriptName(client, clientId):if churn in client:churn = calcChurn(clientId)if engagement in client:engagement = calcEngagement(clientId)if penetration in client:[...]
Answer

Generally, you'd create a mapping of names to functions and use that to calculate the stuff you want:

client_action_map = {'churn': calcChurn,'engagement': calcEngagement,...
}def scriptName(actions, clientId):results = {}for action in actions:results[action] = client_action_map[action](clientId)return results
https://en.xdnf.cn/q/70033.html

Related Q&A

takes 1 positional argument but 2 were given

I would like to run a command line tool to run in a separate function and passed to the button click the additional command for this program but each time I get this as a response.takes 1 positional ar…

With py.test, database is not reset after LiveServerTestCase

I have a number of Django tests and typically run them using py.test. I recently added a new test case in a new file test_selenium.py. This Test Case has uses the LiveServerTestCase and StaticLiveSer…

Using flask wtforms validators without using a form

Im receiving user registration data from an iOS application and Id like to use the validators that come with wtforms to make sure the email and password are acceptable. However, Im not using a flask f…

How to install graph-tool for Anaconda Python 3.5 on linux-64?

Im trying to install graph-tool for Anaconda Python 3.5 on Ubuntu 14.04 (x64), but it turns out thats a real trick.I tried this approach, but run into the problem:The following specifications were foun…

How to quickly encrypt a password string in Django without an User Model?

Based on my current Django app settings, is there a function or a snippet that allows me to view the encrypted password, given a raw string? I am testing some functionality and this would be useful fo…

Embedding multiple gridspec layouts on a single matplotlib figure?

I am using the python graphing library matplotlib to graph several things in a report, and I found myself needing to have several fixed-count graphs above an arbitrary grid of smaller graphs. I search…

Writing integers in binary to file in python

How can I write integers to a file in binary in Python 3?For example, I want to write 6277101735386680763835789423176059013767194773182842284081 to a file in binary in exactly 24 bytes (unsigned, I wi…

Dropping some columns when using to_csv in pandas

I have a data frame which I want to write to tow files, one that contains all of the columns and one that has only a subset of the columns So for this data frame: Out_dataOut[9]: A B …

Setting Max Results in API v4 (python)

In v3 of the API Im seeing that there was a max-results parameter that could be passed to get more than 1000 records. I havent been able to figure out how to pass that parameter in v4 of the API using …

Extract text between two different tags beautiful soup

Im trying to extract the text content of the article from this web page.Im just trying to extract the article content and not the "About the author part".The problem is that all the content a…