Skip to content

Commit b032f42

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: add _COUNT_THOUSANDS/_COUNT_MILLIONS constants to match _SECONDS_PER_MINUTE pattern
format_count used bare 1_000 and 1_000_000 literals six times while the adjacent format_duration already named its thresholds. Replacing the magic numbers with named constants makes the two formatters consistent and removes the repeated literals. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 86829de commit b032f42

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/ralphify/_output.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,19 @@ def format_count(n: int) -> str:
9898
e.g. 999_950 → ``"1.0M"`` instead of ``"1000.0k"`` (same guard
9999
as :func:`format_duration`'s 59.95 → ``"1m 0s"``).
100100
"""
101-
if n >= 1_000_000:
102-
return f"{n / 1_000_000:.1f}M"
103-
if n >= 1_000:
101+
if n >= _COUNT_MILLIONS:
102+
return f"{n / _COUNT_MILLIONS:.1f}M"
103+
if n >= _COUNT_THOUSANDS:
104104
# Use rounded value to avoid "1000.0k" when rounding crosses
105105
# into the next unit (same guard as format_duration's 59.95→1m).
106-
if round(n / 1_000, 1) >= 1_000:
107-
return f"{n / 1_000_000:.1f}M"
108-
return f"{n / 1_000:.1f}k"
106+
if round(n / _COUNT_THOUSANDS, 1) >= _COUNT_THOUSANDS:
107+
return f"{n / _COUNT_MILLIONS:.1f}M"
108+
return f"{n / _COUNT_THOUSANDS:.1f}k"
109109
return str(n)
110110

111111

112+
_COUNT_THOUSANDS = 1_000
113+
_COUNT_MILLIONS = 1_000_000
112114
_SECONDS_PER_MINUTE = 60
113115
_MINUTES_PER_HOUR = 60
114116

0 commit comments

Comments
 (0)