Merge two functions to one - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Merge two functions to one (/thread-39902.html) |
Merge two functions to one - SamLiu - May-03-2023 Hi all, I want to compare with the limit and assign different text colors as below two functions. How to merge two functions(compared_Sens & compared_Noise) to one function apply to all different limits? import pandas as pd import numpy as np sens_delta_check=[ -0.07,-0.13,-0.16,-0.08,-0.11,-0.12,-0.04, -0.06,-0.08,-0.03,0,0,-0.05,-0.01, -0.01,-0.11,-0.05,-0.08,-0.04,-0.06,0.01, -0.17,-0.07,-0.06,-0.14,-0.07,-0.1,-0.05, 0.03,-0.1,-0.05,-0.1,-0.15,0.02,-0.03 ] noise_check=[-104.09,-103.99,-104.09,-103.87,-104.05,-104,-103.98, -103.97,-103.93,-103.99,-103.93,-104.01,-103.98,-103.97, -103.86,-103.93,-104.02,-104.1,-103.97,-103.96,-104, -103.94,-104.01,-104,-103.95,-103.99,-103.97,-103.97, -103.99,-104,-104.04,-103.97,-103.96,-103.95,-103.95] def compared_Sens(val,limit1,limit2,limit3): # 0.3,0.2,0.1 if np.abs(val)>limit1: text_color='Dark Red' elif np.abs(val)>limit2: text_color='Red' elif np.abs(val)>limit3: text_color = 'orange' elif pd.isnull(val): text_color='White' else: text_color='Green' return text_color def compared_Noise(val,limit1,limit2,limit3): # 103.2,102.5,102 if np.abs(val)>limit1: text_color='green' elif np.abs(val)>limit2: text_color='orange' elif np.abs(val)>limit3: text_color = 'red' elif pd.isnull(val): text_color='White' else: text_color='dark red' return text_color print(compared_Sens(sens_delta_check,0.3,0.2,0.1) print(compared_Noise(noise_check,103.2,102.5,102) RE: Merge two functions to one - deanhystad - May-03-2023 Maybe you should provide some examples of an input value, how you would process the value, and what the result should be. I am not seeing how the two existing functions are related. RE: Merge two functions to one - SamLiu - May-03-2023 I appreciate your prompt reply to this. You can see two functions parameters are same, only difference is compare condition such in compared_Sens if val greater than limit1 text_color equal to dark red and greater than limit2 text_color equal to red, greater than limit3 text_color equal to orange, less than limit3 text_color equal to green. But the compared_Noise function compare condition is the same the text_color assign value is the opposite. How to use one function with two conditions, this is what I want. Thanks in advance! RE: Merge two functions to one - deanhystad - May-03-2023 Are you trying to make a generic function that could be used to get a color for the sensor check of the noise check by just passing different parameters? Like this? from random import randint def heat_map(value, map_, null_color="white"): if value is None: return null_color for level, color in map_.items(): if value <= level: return color return color colors = { -4: "red", -3: "orange", -2: "yellow", 1: "green", 2: "yellow", 3: "orange", 4: "red", } for _ in range(20): value = randint(-5, 5) print(value, heat_map(value, colors)) RE: Merge two functions to one - SamLiu - May-05-2023 It's very useful, thanks for your output. |