Add type hints.
David Blume

David Blume commited on 2020-12-30 23:04:36
Showing 4 changed files, with 26 additions and 24 deletions.

... ...
@@ -15,13 +15,13 @@ import signal
15 15
 
16 16
 printer_lock = threading.Lock()
17 17
 
18
-def log_exit(sig, frame):
18
+def log_exit(sig: int, frame) -> None:
19 19
     log(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} '
20 20
         f'PID={os.getpid()} signal={signal.Signals(sig).name} Exiting.')
21 21
     sys.exit(0)
22 22
 
23 23
 
24
-def pinger(host, delay):
24
+def pinger(host: str, delay: float) -> None:
25 25
     """Independent worker thread: repeatedly ping, check, print and sleep."""
26 26
     last_results = None
27 27
     while True:
... ...
@@ -34,12 +34,12 @@ def pinger(host, delay):
34 34
         time.sleep(delay)
35 35
 
36 36
 
37
-def ping(host):
37
+def ping(host: str) -> int:
38 38
     """Returns True if host (str) responds to a ping request."""
39 39
     return subprocess.call([*_ping_cmd, host], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
40 40
 
41 41
 
42
-def log(*args, **kwargs):
42
+def log(*args, **kwargs) -> None:
43 43
     """Opens the logfile, writes the log, and closes the logfile."""
44 44
     # I don't leave the log file open for writing because another process needs access too.
45 45
     with printer_lock:
... ...
@@ -15,14 +15,14 @@ import queue
15 15
 import signal
16 16
 
17 17
 
18
-def log_exit(sig, frame):
18
+def log_exit(sig: int, frame) -> None:
19 19
     _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} '
20 20
                       f'PID={os.getpid()} signal={signal.Signals(sig).name} Exiting.'])
21 21
     _print_queue.join()
22 22
     sys.exit(0)
23 23
 
24 24
 
25
-def pinger(host, delay):
25
+def pinger(host: str, delay: float) -> None:
26 26
     """Independent worker thread: repeatedly ping, check, print and sleep."""
27 27
     last_results = None
28 28
     while True:
... ...
@@ -35,12 +35,12 @@ def pinger(host, delay):
35 35
         time.sleep(delay)
36 36
 
37 37
 
38
-def ping(host):
38
+def ping(host: str) -> int:
39 39
     """Returns True if host (str) responds to a ping request."""
40 40
     return subprocess.call([*_ping_cmd, host], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
41 41
 
42 42
 
43
-def print_manager():
43
+def print_manager() -> None:
44 44
     """The only thread allowed to write output."""
45 45
     while True:
46 46
         job = _print_queue.get()
... ...
@@ -49,11 +49,11 @@ def print_manager():
49 49
         _print_queue.task_done()
50 50
 
51 51
 
52
-def log(*args, **kwargs):
52
+def log(*args, **kwargs) -> None:
53 53
     """Opens the logfile, writes the log, and closes the logfile."""
54 54
     # I don't leave the log file open for writing because another process needs access too.
55 55
     with open(_logname, 'a') as fp:
56
-        return print(*args, **kwargs, file=fp)
56
+        print(*args, **kwargs, file=fp)
57 57
 
58 58
 
59 59
 if __name__ == '__main__':
... ...
@@ -13,18 +13,19 @@ from argparse import ArgumentParser
13 13
 import threading
14 14
 import queue
15 15
 import signal
16
+from typing import Dict, Optional
16 17
 
17 18
 pinger_lock = threading.Lock()
18 19
 
19 20
 
20
-def log_exit(sig, frame):
21
+def log_exit(sig: int, frame) -> None:
21 22
     _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} '
22 23
                       f'PID={os.getpid()} signal={signal.Signals(sig).name} Exiting.'])
23 24
     _print_queue.join()
24 25
     sys.exit(0)
25 26
 
26 27
 
27
-def pinger():
28
+def pinger() -> None:
28 29
     """Each queue item is a new address to ping. Ping and compare and print.
29 30
     Issue: Each of these threads reads from and writes to the global _results dict.
30 31
     """
... ...
@@ -43,12 +44,12 @@ def pinger():
43 44
         _ping_queue.task_done()
44 45
 
45 46
 
46
-def ping(host):
47
+def ping(host: str) -> int:
47 48
     """Returns True if host (str) responds to a ping request."""
48 49
     return subprocess.call([*_ping_cmd, host], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
49 50
 
50 51
 
51
-def print_manager():
52
+def print_manager() -> None:
52 53
     """The only thread allowed to write output."""
53 54
     while True:
54 55
         job = _print_queue.get()
... ...
@@ -57,11 +58,11 @@ def print_manager():
57 58
         _print_queue.task_done()
58 59
 
59 60
 
60
-def log(*args, **kwargs):
61
+def log(*args, **kwargs) -> None:
61 62
     """Opens the logfile, writes the log, and closes the logfile."""
62 63
     # I don't leave the log file open for writing because another process needs access too.
63 64
     with open(_logname, 'a') as fp:
64
-        return print(*args, **kwargs, file=fp)
65
+        print(*args, **kwargs, file=fp)
65 66
 
66 67
 
67 68
 if __name__ == '__main__':
... ...
@@ -70,7 +71,7 @@ if __name__ == '__main__':
70 71
     parser.add_argument('addresses', nargs='+', help='Addresses to ping')
71 72
     parser_args = parser.parse_args()
72 73
 
73
-    _results = dict()
74
+    _results: Dict[str, Optional[int]] = dict()
74 75
     for addr in parser_args.addresses:
75 76
         _results[addr] = None
76 77
     delay = parser_args.delay
... ...
@@ -13,18 +13,19 @@ from argparse import ArgumentParser
13 13
 import threading
14 14
 import queue
15 15
 import signal
16
+from typing import Dict, Optional
16 17
 
17 18
 pinger_lock = threading.Lock()
18 19
 
19 20
 
20
-def log_exit(sig, frame):
21
+def log_exit(sig: int, frame) -> None:
21 22
     _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} '
22 23
                       f'PID={os.getpid()} signal={signal.Signals(sig).name} Exiting.'])
23 24
     _print_queue.join()
24 25
     sys.exit(0)
25 26
 
26 27
 
27
-def pinger(host):
28
+def pinger(host: str) -> None:
28 29
     """Executes one ping, prints if there was a change, exits thread."""
29 30
     now = time.localtime()
30 31
     success = ping(host)
... ...
@@ -38,12 +39,12 @@ def pinger(host):
38 39
         _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", now)} {host} {status}'])
39 40
 
40 41
 
41
-def ping(host):
42
+def ping(host: str) -> int:
42 43
     """Returns True if host (str) responds to a ping request."""
43 44
     return subprocess.call([*_ping_cmd, host], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
44 45
 
45 46
 
46
-def print_manager():
47
+def print_manager() -> None:
47 48
     """ The only thread allowed to write output. """
48 49
     while True:
49 50
         job = _print_queue.get()
... ...
@@ -52,11 +53,11 @@ def print_manager():
52 53
         _print_queue.task_done()
53 54
 
54 55
 
55
-def log(*args, **kwargs):
56
+def log(*args, **kwargs) -> None:
56 57
     """Opens the logfile, writes the log, and closes the logfile."""
57 58
     # I don't leave the log file open for writing because another process needs access too.
58 59
     with open(_logname, 'a') as fp:
59
-        return print(*args, **kwargs, file=fp)
60
+        print(*args, **kwargs, file=fp)
60 61
 
61 62
 
62 63
 if __name__ == '__main__':
... ...
@@ -65,7 +66,7 @@ if __name__ == '__main__':
65 66
     parser.add_argument('addresses', nargs='+', help='Addresses to ping')
66 67
     parser_args = parser.parse_args()
67 68
 
68
-    _results = dict()
69
+    _results: Dict[str, Optional[int]] = dict()
69 70
     for addr in parser_args.addresses:
70 71
         _results[addr] = None
71 72
     delay = parser_args.delay
72 73