Replace the if switch with a lambda and a for loop
This commit is contained in:
parent
51d6d25aa9
commit
9a4c15f533
1 changed files with 8 additions and 14 deletions
20
httping.py
20
httping.py
|
@ -26,28 +26,21 @@ max_ping = datetime.timedelta(milliseconds = 1000) # in seconds
|
||||||
targets = ["https://www.google.de", "https://www.bing.com", "https://www.wikipedia.org"]
|
targets = ["https://www.google.de", "https://www.bing.com", "https://www.wikipedia.org"]
|
||||||
|
|
||||||
max_ping_in_ms = max_ping.total_seconds() * 1000
|
max_ping_in_ms = max_ping.total_seconds() * 1000
|
||||||
|
in_bin = lambda x, bin: x >= bin[0] and x <= bin[1]
|
||||||
|
|
||||||
class Themes:
|
class Themes:
|
||||||
# Themes are from http://colorbrewer2.org/
|
# Themes are from http://colorbrewer2.org/
|
||||||
purple_green = ["#762a83", "#9970ab","#c2a5cf","#a6dba0","#5aae61","#1b7837"]
|
purple_green = ["#762a83", "#9970ab","#c2a5cf","#a6dba0","#5aae61","#1b7837"]
|
||||||
red_green = ["#d73027", "#fc8d59","#fee08b","#d9ef8b","#91cf60","#1a9850"]
|
red_green = ["#d73027", "#fc8d59","#fee08b","#d9ef8b","#91cf60","#1a9850"]
|
||||||
original = ["#acacac","#ff0101","#cc673b","#ce8458","#6bbb15","#0ed812"]
|
original = ["#acacac","#ff0101","#cc673b","#ce8458","#6bbb15","#0ed812"]
|
||||||
|
|
||||||
selected_colors = red_green
|
selected_colors = red_green
|
||||||
|
generate_bins = lambda min_val, max_val, step_width = 2: [[x, x + step_width - 1] for x in range(min_val, max_val, step_width)]
|
||||||
|
bins = generate_bins(0, int(max_ping_in_ms), int(max_ping_in_ms / (len(selected_colors) - 1)))
|
||||||
|
|
||||||
def colorize(response_time):
|
def colorize(response_time):
|
||||||
# panda's "cut" functon would be an alternative here, but this would be overkill, to include numpy just for this
|
for index, bin in enumerate(Themes.bins):
|
||||||
if response_time < max_ping_in_ms / 5:
|
if in_bin(response_time, bin):
|
||||||
return Themes.selected_colors[5]
|
return Themes.selected_colors[len(Themes.selected_colors) - 1 - index]
|
||||||
elif response_time >= max_ping_in_ms / 5 and response_time < 2 * max_ping_in_ms / 5:
|
|
||||||
return Themes.selected_colors[4]
|
|
||||||
elif response_time >= 2 * max_ping_in_ms / 5 and response_time < 3 * max_ping_in_ms / 5:
|
|
||||||
return Themes.selected_colors[3]
|
|
||||||
elif response_time >= 3 * max_ping_in_ms / 5 and response_time < 4 * max_ping_in_ms / 5:
|
|
||||||
return Themes.selected_colors[2]
|
|
||||||
elif response_time >= 4 * max_ping_in_ms / 5 and response_time < max_ping_in_ms:
|
|
||||||
return Themes.selected_colors[1]
|
|
||||||
else:
|
|
||||||
return Themes.selected_colors[0]
|
return Themes.selected_colors[0]
|
||||||
|
|
||||||
def httping(targets):
|
def httping(targets):
|
||||||
|
@ -59,6 +52,7 @@ def httping(targets):
|
||||||
print(str(ex), file=sys.stderr)
|
print(str(ex), file=sys.stderr)
|
||||||
responses[target] = None
|
responses[target] = None
|
||||||
response_times = [value for key, value in responses.items() if isinstance(value, float)]
|
response_times = [value for key, value in responses.items() if isinstance(value, float)]
|
||||||
|
|
||||||
if len(response_times) > 1:
|
if len(response_times) > 1:
|
||||||
return responses, statistics.median(response_times), statistics.stdev(response_times)
|
return responses, statistics.median(response_times), statistics.stdev(response_times)
|
||||||
elif len(response_times) > 0:
|
elif len(response_times) > 0:
|
||||||
|
|
Loading…
Reference in a new issue