Cleanup.
authorScott Gasch <[email protected]>
Wed, 9 Feb 2022 16:59:45 +0000 (08:59 -0800)
committerScott Gasch <[email protected]>
Wed, 9 Feb 2022 16:59:45 +0000 (08:59 -0800)
histogram.py

index f85abea2c45f05ef65c70657bd1b2b6f53f04c04..6f9c0f1e41750f0562293ffedfc4d8383a3f0aa8 100644 (file)
@@ -4,13 +4,26 @@
 """A text-based simple histogram helper class."""
 
 import math
-from typing import Dict, Generic, Iterable, List, Optional, Tuple, TypeVar
+from dataclasses import dataclass
+from typing import Dict, Generic, Iterable, List, NamedTuple, Optional, Tuple, TypeVar
 
 T = TypeVar("T", int, float)
 Bound = int
 Count = int
 
 
+@dataclass
+class BucketDetails:
+    """A collection of details about the internal histogram buckets."""
+
+    num_populated_buckets: int = 0
+    max_population: Optional[int] = None
+    last_bucket_start: Optional[int] = None
+    lowest_start: Optional[int] = None
+    highest_end: Optional[int] = None
+    max_label_width: Optional[int] = None
+
+
 class SimpleHistogram(Generic[T]):
     """A simple histogram."""
 
@@ -74,67 +87,59 @@ class SimpleHistogram(Generic[T]):
             all_true = all_true and self.add_item(item)
         return all_true
 
+    def get_bucket_details(self, label_formatter: str) -> BucketDetails:
+        details = BucketDetails()
+        for (start, end), pop in sorted(self.buckets.items(), key=lambda x: x[0]):
+            if pop > 0:
+                details.num_populated_buckets += 1
+                details.last_bucket_start = start
+                if details.max_population is None or pop > details.max_population:
+                    details.max_population = pop
+                if details.lowest_start is None or start < details.lowest_start:
+                    details.lowest_start = start
+                if details.highest_end is None or end > details.highest_end:
+                    details.highest_end = end
+                label = f'[{label_formatter}..{label_formatter}): ' % (start, end)
+                label_width = len(label)
+                if details.max_label_width is None or label_width > details.max_label_width:
+                    details.max_label_width = label_width
+        return details
+
     def __repr__(self, *, width: int = 80, label_formatter: str = '%d') -> str:
         from text_utils import bar_graph
 
+        details = self.get_bucket_details(label_formatter)
         txt = ""
-        max_population: Optional[int] = None
-        for bucket in self.buckets:
-            pop = self.buckets[bucket]
-            if pop > 0:
-                last_bucket_start = bucket[0]  # beginning of range
-            if max_population is None or pop > max_population:
-                max_population = pop  # bucket with max items
-        if max_population is None:
+        if details.num_populated_buckets == 0:
             return txt
-
-        max_label_width: Optional[int] = None
-        lowest_start: Optional[int] = None
-        highest_end: Optional[int] = None
-        for bucket in sorted(self.buckets, key=lambda x: x[0]):
-            start = bucket[0]
-            if lowest_start is None:
-                lowest_start = start
-            end = bucket[1]
-            if highest_end is None or end > highest_end:
-                highest_end = end
-            label = f'[{label_formatter}..{label_formatter}): ' % (start, end)
-            label_width = len(label)
-            if max_label_width is None or label_width > max_label_width:
-                max_label_width = label_width
-            if start == last_bucket_start:
-                break
-        assert max_label_width is not None
-        assert lowest_start is not None
-        assert highest_end is not None
-
+        assert details.max_label_width is not None
+        assert details.lowest_start is not None
+        assert details.highest_end is not None
+        assert details.max_population is not None
         sigma_label = f'[{label_formatter}..{label_formatter}): ' % (
-            lowest_start,
-            highest_end,
+            details.lowest_start,
+            details.highest_end,
         )
-        if len(sigma_label) > max_label_width:
-            max_label_width = len(sigma_label)
-        bar_width = width - (max_label_width + 16)
+        if len(sigma_label) > details.max_label_width:
+            details.max_label_width = len(sigma_label)
+        bar_width = width - (details.max_label_width + 16)
 
-        for bucket in sorted(self.buckets, key=lambda x: x[0]):
-            start = bucket[0]
-            end = bucket[1]
+        for (start, end), pop in sorted(self.buckets.items(), key=lambda x: x[0]):
             label = f'[{label_formatter}..{label_formatter}): ' % (start, end)
-            pop = self.buckets[bucket]
             bar = bar_graph(
-                (pop / max_population),
+                (pop / details.max_population),
                 include_text=False,
                 width=bar_width,
                 left_end="",
                 right_end="",
             )
-            txt += label.rjust(max_label_width)
+            txt += label.rjust(details.max_label_width)
             txt += bar
             txt += f"({pop/self.count*100.0:5.2f}% n={pop})\n"
-            if start == last_bucket_start:
+            if start == details.last_bucket_start:
                 break
         txt += '-' * width + '\n'
-        txt += sigma_label.rjust(max_label_width)
+        txt += sigma_label.rjust(details.max_label_width)
         txt += ' ' * (bar_width - 2)
         txt += f'Σ=(100.00% n={self.count})\n'
         return txt