diff options
| author | Scott <[email protected]> | 2022-01-31 09:55:30 -0800 |
|---|---|---|
| committer | Scott <[email protected]> | 2022-01-31 09:55:30 -0800 |
| commit | f41c2bbae228ea2305e7530148c4d2dca5131200 (patch) | |
| tree | 04ae4f0db5a554d3854d7700c14ce2494653cc74 /histogram.py | |
| parent | 9313acd7f50f19dfa3a3ff32f686c924b4cfe75a (diff) | |
Make histogram auto-format labels.
Diffstat (limited to 'histogram.py')
| -rw-r--r-- | histogram.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/histogram.py b/histogram.py index 7be643f..87ef773 100644 --- a/histogram.py +++ b/histogram.py @@ -67,7 +67,7 @@ class SimpleHistogram(Generic[T]): all_true = all_true and self.add_item(item) return all_true - def __repr__(self, width: int = 80, *, label_formatter: str = '%5s') -> str: + def __repr__(self, *, width: int = 80, label_formatter: str = None) -> str: from text_utils import bar_graph max_population: Optional[int] = None @@ -92,6 +92,13 @@ class SimpleHistogram(Generic[T]): end = bucket[1] if highest_end is None or end > highest_end: highest_end = end + if label_formatter is None: + if type(start) == int and type(end) == int: + label_formatter = '%d' + elif type(start) == float and type(end) == float: + label_formatter = '%.2f' + else: + label_formatter = '%s' label = f'[{label_formatter}..{label_formatter}): ' % (start, end) label_width = len(label) if max_label_width is None or label_width > max_label_width: |
