From 80902ae5f6f1e499dc9ce4962f9b7e536d9803b4 Mon Sep 17 00:00:00 2001
From: Alexandru Fikl <alexfikl@gmail.com>
Date: Mon, 24 Jun 2024 16:36:19 +0300
Subject: [PATCH] stopwatch: add type annotations

---
 pytools/stopwatch.py | 50 +++++++++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/pytools/stopwatch.py b/pytools/stopwatch.py
index 5103d83..d0c9234 100644
--- a/pytools/stopwatch.py
+++ b/pytools/stopwatch.py
@@ -1,25 +1,28 @@
 import time
+from typing import List, Optional
 
-import pytools
+from pytools import DependentDictionary, Reference
 
 
 class StopWatch:
-    def __init__(self):
-        self.Elapsed = 0.
-        self.LastStart = None
+    def __init__(self) -> None:
+        self.Elapsed = 0.0
+        self.LastStart: Optional[float] = None
 
-    def start(self):
+    def start(self) -> "StopWatch":
         assert self.LastStart is None
+
         self.LastStart = time.time()
         return self
 
-    def stop(self):
+    def stop(self) -> "StopWatch":
         assert self.LastStart is not None
+
         self.Elapsed += time.time() - self.LastStart
         self.LastStart = None
         return self
 
-    def elapsed(self):
+    def elapsed(self) -> float:
         if self.LastStart:
             return time.time() - self.LastStart + self.Elapsed
         else:
@@ -27,19 +30,21 @@ class StopWatch:
 
 
 class Job:
-    def __init__(self, name):
+    def __init__(self, name: str) -> None:
         self.Name = name
         self.StopWatch = StopWatch().start()
+
         if self.is_visible():
             print(f"{name}...")
 
-    def done(self):
+    def done(self) -> None:
         elapsed = self.StopWatch.elapsed()
+
         JOB_TIMES[self.Name] += elapsed
         if self.is_visible():
             print(" " * (len(self.Name) + 2), elapsed, "seconds")
 
-    def is_visible(self):
+    def is_visible(self) -> bool:
         if PRINT_JOBS.get():
             return self.Name not in HIDDEN_JOBS
         else:
@@ -47,26 +52,27 @@ class Job:
 
 
 class EtaEstimator:
-    def __init__(self, total_steps):
+    def __init__(self, total_steps: int) -> None:
         self.stopwatch = StopWatch().start()
         self.total_steps = total_steps
         assert total_steps > 0
 
-    def estimate(self, done):
-        fraction_done = done/self.total_steps
+    def estimate(self, done: int) -> Optional[float]:
+        fraction_done = done / self.total_steps
         time_spent = self.stopwatch.elapsed()
-        if fraction_done > 1e-5:
-            return time_spent/fraction_done-time_spent
+
+        if fraction_done > 1.0e-5:
+            return time_spent / fraction_done - time_spent
         else:
             return None
 
 
-def print_job_summary():
-    for key in JOB_TIMES:
-        print(key, " " * (50-len(key)), JOB_TIMES[key])
+def print_job_summary() -> None:
+    for key, value in JOB_TIMES.iteritems():
+        print(key, " " * (50 - len(key)), value)
 
 
-HIDDEN_JOBS = []
-VISIBLE_JOBS = []
-JOB_TIMES = pytools.DependentDictionary(lambda x: 0)
-PRINT_JOBS = pytools.Reference(True)
+HIDDEN_JOBS: List[str] = []
+VISIBLE_JOBS: List[str] = []
+JOB_TIMES = DependentDictionary(lambda x: 0)
+PRINT_JOBS = Reference(True)
-- 
GitLab