Skip to content

Commit 6098b74

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: extract magic numbers as named constants in _output and _frontmatter
Replace hardcoded `60` in format_duration with _SECONDS_PER_MINUTE and _MINUTES_PER_HOUR constants. Extract UTF-8 BOM character literal as _UTF8_BOM constant and use str.removeprefix for clearer intent. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 1403d11 commit 6098b74

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

src/ralphify/_frontmatter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
# Human-readable description of allowed name characters, paired with CMD_NAME_RE.
4343
VALID_NAME_CHARS_MSG = "Names may only contain letters, digits, hyphens, and underscores."
4444

45+
# UTF-8 BOM character — files saved on Windows may start with this.
46+
_UTF8_BOM = "\ufeff"
47+
4548
# Pre-compiled pattern to strip HTML comments from body text.
4649
_HTML_COMMENT_RE = re.compile(r"<!--.*?-->", re.DOTALL)
4750

@@ -77,8 +80,8 @@ def parse_frontmatter(text: str) -> tuple[dict[str, Any], str]:
7780
7881
Returns ``(frontmatter_dict, body_text)``.
7982
"""
80-
if text.startswith("\ufeff"):
81-
text = text[1:]
83+
if text.startswith(_UTF8_BOM):
84+
text = text.removeprefix(_UTF8_BOM)
8285
fm_raw, body = _extract_frontmatter_block(text)
8386
if fm_raw:
8487
try:

src/ralphify/_output.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,25 @@ def collect_output(
6464
return "".join(parts)
6565

6666

67+
_SECONDS_PER_MINUTE = 60
68+
_MINUTES_PER_HOUR = 60
69+
70+
6771
def format_duration(seconds: float) -> str:
6872
"""Format *seconds* as a compact human-readable duration string.
6973
7074
Returns ``"12.3s"`` for sub-minute, ``"2m 30s"`` for sub-hour,
7175
and ``"1h 15m"`` for longer durations. Used in CLI output and
7276
event data for iteration timing.
7377
"""
74-
if round(seconds, 1) < 60:
78+
if round(seconds, 1) < _SECONDS_PER_MINUTE:
7579
return f"{seconds:.1f}s"
7680
# Use rounded total to avoid edge cases like 59.95 → "0m 60s"
7781
total = round(seconds)
78-
minutes = total // 60
79-
secs = total % 60
80-
if minutes < 60:
82+
minutes = total // _SECONDS_PER_MINUTE
83+
secs = total % _SECONDS_PER_MINUTE
84+
if minutes < _MINUTES_PER_HOUR:
8185
return f"{minutes}m {secs}s"
82-
hours = minutes // 60
83-
mins = minutes % 60
86+
hours = minutes // _MINUTES_PER_HOUR
87+
mins = minutes % _MINUTES_PER_HOUR
8488
return f"{hours}h {mins}m"

0 commit comments

Comments
 (0)