types: no need for Union[int, float], and handle fact os.cpu_count() could be None.
David Blume

David Blume commited on 2021-01-09 23:53:23
Showing 2 changed files, with 5 additions and 3 deletions.

... ...
@@ -12,10 +12,12 @@ from typing import Union, Optional, Tuple, List, Callable
12 12
 
13 13
 
14 14
 def profile(sort: Union[Tuple, List, str]='cumulative',
15
-            lines_to_print: Union[int, float]=20,
15
+            lines_to_print: float=20,
16 16
             strip_dirs: bool=True,
17 17
             filename: Optional[str]=None) -> Callable:
18 18
     """A decorator which profiles a callable.
19
+    If lines_to_print is an int, it's the number of lines. If it's a float
20
+    then it's the percentage of total lines. (Eg., .2 = 20%)
19 21
     Output goes to stdout if filename is None, otherwise to filename."""
20 22
     def outer(fun: Callable) -> Callable:
21 23
         def inner(*args, **kwargs):
... ...
@@ -43,5 +45,5 @@ def profile(sort: Union[Tuple, List, str]='cumulative',
43 45
     if hasattr(sort, '__call__'):
44 46
         fun = sort
45 47
         sort = 'cumulative'
46
-        outer = outer(fun)
48
+        outer = outer(fun)  # type: ignore
47 49
     return outer
... ...
@@ -38,7 +38,7 @@ def sitesize(url: str) -> Tuple[str, Union[int, str]]:
38 38
 
39 39
 def run() -> None:
40 40
     # Network bound: Use max(); CPU bound: Use min()
41
-    pool = ThreadPool(max(len(sites), os.cpu_count()))
41
+    pool = ThreadPool(max(len(sites), os.cpu_count() is None and 1 or os.cpu_count()))  # type: ignore
42 42
     for result in pool.imap_unordered(sitesize, sites):
43 43
         print(result)
44 44
 
45 45