formatting
Centralized formatting utilities for snakesee.
This module provides consistent formatting functions for durations, sizes, percentages, and status display throughout the application.
Classes¶
StatusColor ¶
Bases: str, Enum
Standard colors for workflow/job status display.
Source code in snakesee/formatting.py
StatusStyle
dataclass
¶
Complete styling for a status indicator.
Source code in snakesee/formatting.py
Functions¶
format_count ¶
Format a count as "X of Y (Z%)".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current
|
int
|
Current count. |
required |
total
|
int
|
Total count. |
required |
include_percentage
|
bool
|
Whether to include percentage. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted count string. |
Examples:
>>> format_count(5, 10)
'5 of 10 (50.0%)'
>>> format_count(5, 10, include_percentage=False)
'5 of 10'
Source code in snakesee/formatting.py
format_count_compact ¶
Format a count as "X/Y".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current
|
int
|
Current count. |
required |
total
|
int
|
Total count. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Compact count string. |
Examples:
Source code in snakesee/formatting.py
format_duration ¶
format_duration(seconds: float, precision: Literal['auto', 'seconds', 'minutes', 'hours'] = 'auto') -> str
Format seconds as human-readable duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
Duration in seconds. |
required |
precision
|
Literal['auto', 'seconds', 'minutes', 'hours']
|
Level of precision for output. - "auto": Choose based on magnitude - "seconds": Always include seconds - "minutes": Include minutes, drop seconds for large values - "hours": Only show hours for very large values |
'auto'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted duration string (e.g., "5s", "2m 30s", "1h 15m"). |
Examples:
>>> format_duration(45)
'45s'
>>> format_duration(125)
'2m 5s'
>>> format_duration(3725)
'1h 2m'
>>> format_duration(float("inf"))
'unknown'
Source code in snakesee/formatting.py
format_duration_range ¶
Format a duration range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
float
|
Lower bound in seconds. |
required |
upper
|
float
|
Upper bound in seconds. |
required |
separator
|
str
|
String between lower and upper. |
' - '
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted range string (e.g., "2m - 5m"). |
Examples:
>>> format_duration_range(60, 180)
'1m - 3m'
>>> format_duration_range(30, 90)
'30s - 1m 30s'
>>> format_duration_range(3600, 7200)
'1h - 2h'
Source code in snakesee/formatting.py
format_eta ¶
format_eta(seconds_remaining: float, lower_bound: float | None = None, upper_bound: float | None = None, confidence: float = 1.0) -> str
Format an ETA with optional range and confidence indication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds_remaining
|
float
|
Expected seconds remaining. |
required |
lower_bound
|
float | None
|
Optimistic estimate (optional). |
None
|
upper_bound
|
float | None
|
Pessimistic estimate (optional). |
None
|
confidence
|
float
|
Confidence level (0.0 to 1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted ETA string with appropriate caveats. |
Examples:
>>> format_eta(300, confidence=0.9)
'~5m'
>>> format_eta(300, lower_bound=180, upper_bound=420, confidence=0.5)
'3m - 7m'
>>> format_eta(300, confidence=0.2)
'~5m (rough)'
Source code in snakesee/formatting.py
format_percentage ¶
Format a value as a percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Value between 0.0 and 1.0 (or 0-100 if > 1). |
required |
precision
|
int
|
Decimal places. |
1
|
include_symbol
|
bool
|
Whether to include the % symbol. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted percentage string. |
Examples:
>>> format_percentage(0.756)
'75.6%'
>>> format_percentage(0.756, precision=0)
'76%'
>>> format_percentage(0.756, include_symbol=False)
'75.6'
Source code in snakesee/formatting.py
format_size ¶
Format bytes as human-readable size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bytes_
|
int | float
|
Size in bytes. |
required |
precision
|
int
|
Decimal places for non-integer results. |
1
|
binary
|
bool
|
Use binary (1024) or SI (1000) units. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted size string (e.g., "1.5 GB", "256 KB"). |
Examples:
Source code in snakesee/formatting.py
format_size_rate ¶
Format bytes per second as human-readable rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bytes_per_second
|
float
|
Rate in bytes per second. |
required |
precision
|
int
|
Decimal places for output. |
1
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted rate string (e.g., "1.5 MB/s"). |
Examples:
>>> format_size_rate(1048576)
'1 MB/s'
>>> format_size_rate(1536)
'1.5 KB/s'
>>> format_size_rate(0)
'0 B/s'
Source code in snakesee/formatting.py
format_wildcards ¶
format_wildcards(wildcards: dict[str, str] | None, separator: str = ', ', max_length: int | None = None) -> str
Format wildcards dictionary as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wildcards
|
dict[str, str] | None
|
Dictionary of wildcard key-value pairs. |
required |
separator
|
str
|
String between key=value pairs. |
', '
|
max_length
|
int | None
|
Maximum output length (truncate with "..."). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted wildcards string. |
Examples:
>>> format_wildcards({"sample": "A", "batch": "1"})
'sample=A, batch=1'
>>> format_wildcards(None)
''
Source code in snakesee/formatting.py
get_status_color ¶
Get just the color for a status (backward compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
Status string (case-insensitive). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Color name for Rich markup. |
get_status_style ¶
get_status_style(status: str) -> StatusStyle
Get the style for a given status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
Status string (case-insensitive). |
required |
Returns:
| Type | Description |
|---|---|
StatusStyle
|
StatusStyle for the status, or default unknown style. |