Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge two functions to one
#1
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)
Reply
#2
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.
Reply
#3
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!
Reply
#4
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))
SamLiu likes this post
Reply
#5
It's very useful, thanks for your output.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020