incorporated some of the findings of pylint

This commit is contained in:
Tobias Schmidl 2019-06-13 05:21:32 +02:00
parent 3fa40d3371
commit 1278ad984c

View file

@ -22,35 +22,52 @@ __license__ = "MIT"
__version__ = "0.0.1" __version__ = "0.0.1"
__maintainer__ = "Tobias Schmidl" __maintainer__ = "Tobias Schmidl"
max_ping = datetime.timedelta(milliseconds=1000) # in seconds 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]
def in_bucket(element, bucket):
""" Returns True if element is in bucket """
return bucket[0] <= element <= bucket[1]
def generate_buckets(min_val, max_val, step_width=2):
""" Generates a list of partitioned lists between min_val and max_val."""
return [[x, x + step_width - 1] for x in range(min_val, max_val, step_width)]
class Themes: class Themes:
"""Maintains a list of themes and generates buckets between 0 and MAX_PING_IN_MS based on the selected theme."""
# 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 = None
generate_bins = lambda min_val, max_val, step_width=2: [[x, x + step_width - 1] for x in range( buckets = None
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 __init__(self, selected_colors):
for index, bin in enumerate(Themes.bins): self.selected_colors = selected_colors
if in_bin(response_time, bin): self.buckets = generate_buckets(0, int(MAX_PING_IN_MS), int(MAX_PING_IN_MS / (len(selected_colors) - 1)))
return Themes.selected_colors[len(Themes.selected_colors) - 1 - index]
return Themes.selected_colors[0] def colorize(self, input_text):
"""Colorized the current input according to the selected theme."""
for index, current_bucket in enumerate(self.buckets):
if in_bucket(input_text, current_bucket):
return self.selected_colors[len(self.selected_colors) - 1 - index]
return self.selected_colors[0]
CURRENT_THEME = Themes(Themes.red_green)
def httping(targets): def httping(targets):
"""Pings the supplied targets and returns a mean and a std deviation over all target responses."""
responses = {} responses = {}
for target in targets: for target in targets:
try: try:
responses[target] = requests.head(target, timeout=max_ping.total_seconds()).elapsed.total_seconds() * 1000 responses[target] = requests.head(target, timeout=MAX_PING.total_seconds()).elapsed.total_seconds() * 1000
except requests.exceptions.RequestException as ex: except requests.exceptions.RequestException as ex:
print(str(ex), file=sys.stderr) print(str(ex), file=sys.stderr)
responses[target] = None responses[target] = None
@ -58,21 +75,22 @@ def httping(targets):
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 response_times:
return responses, response_times[0], 0 return responses, response_times[0], 0
else: else:
return responses, None, None return responses, None, None
if __name__ == "__main__": if __name__ == "__main__":
response_times, median, stddev = httping(targets) responses, median, stddev = httping(TARGETS)
if isinstance(median, float) and median != max_ping_in_ms: if isinstance(median, float) and median != MAX_PING_IN_MS:
print("⦿ " + str(round(median, 2)) + "±" + str(round(stddev, 2)) + " | color=" + Themes.colorize(median)) print("⦿ " + str(round(median, 2)) + "±" + str(round(stddev, 2)) + " | color=" + CURRENT_THEME.colorize(median))
else: else:
print("☠ | color=" + Themes.selected_colors[0]) print("☠ | color=" + CURRENT_THEME.selected_colors[0])
print("---") print("---")
for target, response_time in response_times.items(): for current_host, current_response in responses.items():
if isinstance(response_time, float): if isinstance(current_response, float):
print(target + ": " + str(round(response_time, 2)) + " | color=" + Themes.colorize(response_time)) print(current_host + ": " + str(round(current_response, 2)) + " | color=" +
CURRENT_THEME.colorize(current_response))
else: else:
print(target + ": ☠ | color=" + Themes.selected_colors[0]) print(current_host + ": ☠ | color=" + CURRENT_THEME.selected_colors[0])