David Blume commited on 2020-12-30 19:35:11
Showing 3 changed files, with 14 additions and 11 deletions.
... | ... |
@@ -3,11 +3,11 @@ |
3 | 3 |
import threading, queue |
4 | 4 |
|
5 | 5 |
|
6 |
-_counter = 0 |
|
6 |
+_counter:int = 0 |
|
7 | 7 |
|
8 | 8 |
counter_queue = queue.Queue() |
9 | 9 |
|
10 |
-def counter_manager(): |
|
10 |
+def counter_manager() -> None: |
|
11 | 11 |
'I have EXCLUSIVE rights to update the _counter variable' |
12 | 12 |
global _counter |
13 | 13 |
|
... | ... |
@@ -22,7 +22,7 @@ def counter_manager(): |
22 | 22 |
|
23 | 23 |
print_queue = queue.Queue() |
24 | 24 |
|
25 |
-def print_manager(): |
|
25 |
+def print_manager() -> None: |
|
26 | 26 |
'I have EXCLUSIVE rights to call the "print" keyword' |
27 | 27 |
while True: |
28 | 28 |
job = print_queue.get() |
... | ... |
@@ -31,12 +31,12 @@ def print_manager(): |
31 | 31 |
print_queue.task_done() |
32 | 32 |
|
33 | 33 |
|
34 |
-def worker(): |
|
34 |
+def worker() -> None: |
|
35 | 35 |
'My job is to increment the counter and print the current count' |
36 | 36 |
counter_queue.put(1) |
37 | 37 |
|
38 | 38 |
|
39 |
-def run(): |
|
39 |
+def run() -> None: |
|
40 | 40 |
# Set up the counter manager |
41 | 41 |
t = threading.Thread(target=counter_manager) |
42 | 42 |
t.daemon = True |
... | ... |
@@ -4,8 +4,9 @@ import os |
4 | 4 |
import urllib.request |
5 | 5 |
import urllib.error |
6 | 6 |
from multiprocessing.pool import ThreadPool |
7 |
+from typing import List, Tuple, Union |
|
7 | 8 |
|
8 |
-sites = [ |
|
9 |
+sites: List[str] = [ |
|
9 | 10 |
# 'https://www.yahoo.com/', |
10 | 11 |
# 'http://www.cnn.com', |
11 | 12 |
# 'http://www.python.org', |
... | ... |
@@ -23,7 +24,7 @@ sites = [ |
23 | 24 |
] |
24 | 25 |
|
25 | 26 |
|
26 |
-def sitesize(url: str): |
|
27 |
+def sitesize(url: str) -> Tuple[str, Union[int, str]]: |
|
27 | 28 |
''' Determine the size of a website ''' |
28 | 29 |
try: |
29 | 30 |
with urllib.request.urlopen(url) as f: |
... | ... |
@@ -32,10 +33,10 @@ def sitesize(url: str): |
32 | 33 |
except urllib.error.HTTPError as e: |
33 | 34 |
return url, str(e) |
34 | 35 |
except urllib.error.URLError as e: |
35 |
- return url, "On macOS? Try another python3. " + str(e) |
|
36 |
+ return url, str(e) |
|
36 | 37 |
|
37 | 38 |
|
38 |
-def run(): |
|
39 |
+def run() -> None: |
|
39 | 40 |
# Network bound: Use max(); CPU bound: Use min() |
40 | 41 |
pool = ThreadPool(max(len(sites), os.cpu_count())) |
41 | 42 |
for result in pool.imap_unordered(sitesize, sites): |
... | ... |
@@ -9,6 +9,7 @@ import functools |
9 | 9 |
from argparse import ArgumentParser |
10 | 10 |
import counter.counter |
11 | 11 |
import sitesize.sitesize |
12 |
+from typing import Optional, Callable |
|
12 | 13 |
|
13 | 14 |
|
14 | 15 |
def timeit(f): |
... | ... |
@@ -22,7 +23,8 @@ def timeit(f): |
22 | 23 |
return wrapper |
23 | 24 |
|
24 | 25 |
|
25 |
-def set_v_print(verbose: bool): |
|
26 |
+v_print:Callable |
|
27 |
+def set_v_print(verbose: bool) -> None: |
|
26 | 28 |
""" |
27 | 29 |
Defines the function v_print. |
28 | 30 |
It prints if verbose is true, otherwise, it does nothing. |
... | ... |
@@ -51,7 +53,7 @@ class Coffee: |
51 | 53 |
|
52 | 54 |
|
53 | 55 |
@timeit |
54 |
-def main(debug: bool): |
|
56 |
+def main(debug: bool) -> None: |
|
55 | 57 |
script_dir = os.path.abspath(os.path.dirname(sys.argv[0])) |
56 | 58 |
print(f'{sys.argv[0]} is in {script_dir}.') |
57 | 59 |
|
58 | 60 |