Added yapf config, applied yapf

This commit is contained in:
Tobias Schmidl 2019-06-13 04:17:07 +02:00
parent 9a4c15f533
commit 3fa40d3371
3 changed files with 49 additions and 13 deletions

2
.gitignore vendored
View file

@ -102,3 +102,5 @@ venv.bak/
# mypy # mypy
.mypy_cache/ .mypy_cache/
/tags
/tags.*

30
.style.yapf Normal file
View file

@ -0,0 +1,30 @@
[style]
# The column limit.
column_limit=120
# Split before the '.' if we need to split a longer expression:
#
# foo = ('This is a really long string: {}, {}, {}, {}'.format(a, b, c, d))
#
# would reformat to something like:
#
# foo = ('This is a really long string: {}, {}, {}, {}'
# .format(a, b, c, d))
split_before_dot=False
# Set to True to split list comprehensions and generators that have
# non-trivial expressions and multiple clauses before each of these
# clauses. For example:
#
# result = [
# a_long_var + 100 for a_long_var in xrange(1000)
# if a_long_var % 10]
#
# would reformat to something like:
#
# result = [
# a_long_var + 100
# for a_long_var in xrange(1000)
# if a_long_var % 10]
split_complex_comprehension=True

View file

@ -1,6 +1,5 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" Simulates httping for `argos <https://github.com/p-e-w/argos>`""" """ Simulates httping for `argos <https://github.com/p-e-w/argos>`"""
# <bitbar.title>httping</bitbar.title> # <bitbar.title>httping</bitbar.title>
# <bitbar.version>v1.0</bitbar.version> # <bitbar.version>v1.0</bitbar.version>
@ -11,10 +10,11 @@
# <bitbar.dependencies>python,requests</bitbar.dependencies> # <bitbar.dependencies>python,requests</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/schtobia/argos-httping/blob/master/httping.py</bitbar.abouturl> # <bitbar.abouturl>https://github.com/schtobia/argos-httping/blob/master/httping.py</bitbar.abouturl>
import sys #for redirecting print to stderr import datetime # for the whole ms → s and vice versa stuff
import datetime #for the whole ms → s and vice versa stuff import statistics # for median and stdev
import requests #for the central HTTP HEAD request import sys # for redirecting print to stderr
import statistics #for median and stdev
import requests # for the central HTTP HEAD request
__author__ = "Tobias Schmidl" __author__ = "Tobias Schmidl"
__copyright__ = "Copyright 2018, Tobias Schmidl" __copyright__ = "Copyright 2018, Tobias Schmidl"
@ -22,19 +22,21 @@ __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] 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)] 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))) 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):
@ -43,11 +45,12 @@ class Themes:
return Themes.selected_colors[len(Themes.selected_colors) - 1 - index] return Themes.selected_colors[len(Themes.selected_colors) - 1 - index]
return Themes.selected_colors[0] return Themes.selected_colors[0]
def httping(targets): def httping(targets):
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
@ -60,12 +63,13 @@ def httping(targets):
else: else:
return responses, None, None return responses, None, None
if __name__ == "__main__": if __name__ == "__main__":
response_times, median, stddev = httping(targets) response_times, 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=" + Themes.colorize(median))
else: else:
print("☠ | color=" + Themes.selected_colors[0]); print("☠ | color=" + Themes.selected_colors[0])
print("---") print("---")
for target, response_time in response_times.items(): for target, response_time in response_times.items():
if isinstance(response_time, float): if isinstance(response_time, float):