Extended type annotations
This commit is contained in:
parent
9a096252c4
commit
1dd63d542e
1 changed files with 22 additions and 16 deletions
38
httping.py
38
httping.py
|
@ -1,5 +1,6 @@
|
|||
#! /usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Simulates httping for `argos <https://github.com/p-e-w/argos>`"""
|
||||
# <bitbar.title>httping</bitbar.title>
|
||||
# <bitbar.version>v1.0</bitbar.version>
|
||||
|
@ -10,13 +11,14 @@
|
|||
# <bitbar.dependencies>python,requests</bitbar.dependencies>
|
||||
# <bitbar.abouturl>https://github.com/schtobia/argos-httping/blob/master/httping.py</bitbar.abouturl>
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime # for the whole ms → s and vice versa stuff
|
||||
import statistics # for median and stdev
|
||||
import sys # for redirecting print to stderr
|
||||
from typing import Generic, List, Sequence, TypeVar, Optional, Tuple, Dict, Union
|
||||
|
||||
import requests # for the central HTTP HEAD request
|
||||
from typing import (List)
|
||||
from __future__ import annotations
|
||||
|
||||
__author__ = "Tobias Schmidl"
|
||||
__copyright__ = "Copyright 2018, Tobias Schmidl"
|
||||
|
@ -24,30 +26,34 @@ __license__ = "MIT"
|
|||
__version__ = "0.0.1"
|
||||
__maintainer__ = "Tobias Schmidl"
|
||||
|
||||
MAX_PING: int = datetime.timedelta(milliseconds=1000) # in seconds
|
||||
MAX_PING: datetime.timedelta = datetime.timedelta(milliseconds=1000) # in seconds
|
||||
TARGETS: List[str] = ["https://www.google.de", "https://www.bing.com", "https://www.wikipedia.org"]
|
||||
|
||||
MAX_PING_IN_MS: int = MAX_PING.total_seconds() * 1000
|
||||
T = TypeVar('T')
|
||||
Bucket = Tuple[int, int]
|
||||
BucketList = List[Bucket]
|
||||
|
||||
MAX_PING_IN_MS: int = int(MAX_PING.total_seconds() * 1000)
|
||||
|
||||
|
||||
def in_bucket(element, bucket):
|
||||
def in_bucket(element: int, bucket: Bucket) -> bool:
|
||||
""" Returns True if element is in bucket """
|
||||
return bucket[0] <= element <= bucket[1]
|
||||
|
||||
|
||||
def generate_buckets(min_val, max_val, step_width=2):
|
||||
def generate_buckets(min_val: int, max_val: int, step_width: int = 2) -> BucketList:
|
||||
""" 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)]
|
||||
return [(x, x + step_width - 1) for x in range(min_val, max_val, step_width)]
|
||||
|
||||
|
||||
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/
|
||||
purple_green = ["#762a83", "#9970ab", "#c2a5cf", "#a6dba0", "#5aae61", "#1b7837"]
|
||||
red_green = ["#d73027", "#fc8d59", "#fee08b", "#d9ef8b", "#91cf60", "#1a9850"]
|
||||
original = ["#acacac", "#ff0101", "#cc673b", "#ce8458", "#6bbb15", "#0ed812"]
|
||||
selected_colors = None
|
||||
buckets = None
|
||||
purple_green: List[str] = ["#762a83", "#9970ab", "#c2a5cf", "#a6dba0", "#5aae61", "#1b7837"]
|
||||
red_green: List[str] = ["#d73027", "#fc8d59", "#fee08b", "#d9ef8b", "#91cf60", "#1a9850"]
|
||||
original: List[str] = ["#acacac", "#ff0101", "#cc673b", "#ce8458", "#6bbb15", "#0ed812"]
|
||||
selected_colors: List[str] = []
|
||||
buckets: BucketList = []
|
||||
|
||||
def __init__(self, selected_colors):
|
||||
self.selected_colors = selected_colors
|
||||
|
@ -61,12 +67,12 @@ class Themes:
|
|||
return self.selected_colors[0]
|
||||
|
||||
|
||||
CURRENT_THEME = Themes(Themes.red_green)
|
||||
CURRENT_THEME: Themes = Themes(Themes.red_green)
|
||||
|
||||
|
||||
def httping(targets):
|
||||
def httping(targets: List[str]) -> Tuple[Dict[str, Optional[float]], Optional[float], Optional[float]]:
|
||||
"""Pings the supplied targets and returns a mean and a std deviation over all target responses."""
|
||||
responses = {}
|
||||
responses : Dict[str, Optional[float]] = {}
|
||||
for target in targets:
|
||||
try:
|
||||
responses[target] = requests.head(target, timeout=MAX_PING.total_seconds()).elapsed.total_seconds() * 1000
|
||||
|
@ -86,7 +92,7 @@ def httping(targets):
|
|||
if __name__ == "__main__":
|
||||
responses, median, stddev = httping(TARGETS)
|
||||
if isinstance(median, float) and median != MAX_PING_IN_MS:
|
||||
print("⦿ " + str(round(median, 2)) + "±" + str(round(stddev, 2)) + " | color=" + CURRENT_THEME.colorize(median))
|
||||
print("⦿ " + str(round(median, 2)) + "±" + str(round(stddev, 2)) + " | color=" + CURRENT_THEME.colorize(median)) # type: ignore
|
||||
else:
|
||||
print("☠ | color=" + CURRENT_THEME.selected_colors[0])
|
||||
print("---")
|
||||
|
|
Loading…
Reference in a new issue