Formatting
provide.foundation.formatting
¶
TODO: Add module docstring.
Functions¶
format_duration
¶
Format seconds as human-readable duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
Duration in seconds |
required |
short
|
bool
|
Use short format (1h30m vs 1 hour 30 minutes) |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Human-readable duration string |
Examples:
>>> format_duration(90)
'1 minute 30 seconds'
>>> format_duration(90, short=True)
'1m30s'
>>> format_duration(3661)
'1 hour 1 minute 1 second'
>>> format_duration(3661, short=True)
'1h1m1s'
Source code in provide/foundation/formatting/numbers.py
format_grouped
¶
Format a string with grouping separators for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to format |
required |
group_size
|
int
|
Number of characters per group |
8
|
groups
|
int
|
Number of groups to show (0 for all) |
0
|
separator
|
str
|
Separator between groups |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with groups |
Examples:
>>> format_grouped("abc123def456", group_size=4, separator="-")
'abc1-23de-f456'
>>> format_grouped("abc123def456", group_size=4, groups=2)
'abc1 23de'
>>> format_grouped("1234567890abcdef", group_size=4)
'1234 5678 90ab cdef'
Source code in provide/foundation/formatting/grouping.py
format_number
¶
Format number with thousands separators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
float
|
Number to format |
required |
precision
|
int | None
|
Decimal places (None for automatic) |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted number string |
Examples:
Source code in provide/foundation/formatting/numbers.py
format_percentage
¶
Format value as percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Value to format (0.5 = 50%) |
required |
precision
|
int
|
Decimal places |
1
|
include_sign
|
bool
|
Include + sign for positive values |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted percentage string |
Examples:
>>> format_percentage(0.5)
'50.0%'
>>> format_percentage(0.1234, precision=2)
'12.34%'
>>> format_percentage(0.05, include_sign=True)
'+5.0%'
Source code in provide/foundation/formatting/numbers.py
format_size
¶
Format bytes as human-readable size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_bytes
|
float
|
Size in bytes |
required |
precision
|
int
|
Decimal places for display |
1
|
Returns:
| Type | Description |
|---|---|
str
|
Human-readable size string |
Examples:
>>> format_size(1024)
'1.0 KB'
>>> format_size(1536)
'1.5 KB'
>>> format_size(1073741824)
'1.0 GB'
>>> format_size(0)
'0 B'
Source code in provide/foundation/formatting/numbers.py
format_table
¶
format_table(
headers: list[str],
rows: list[list[Any]],
alignment: list[str] | None = None,
) -> str
Format data as ASCII table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers
|
list[str]
|
Column headers |
required |
rows
|
list[list[Any]]
|
Data rows |
required |
alignment
|
list[str] | None
|
Column alignments ('l', 'r', 'c') |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted table string |
Examples:
>>> headers = ['Name', 'Age']
>>> rows = [['Alice', 30], ['Bob', 25]]
>>> print(format_table(headers, rows))
Name | Age
------|----
Alice | 30
Bob | 25
Source code in provide/foundation/formatting/tables.py
indent
¶
Indent text lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to indent |
required |
spaces
|
int
|
Number of spaces to indent |
2
|
first_line
|
bool
|
Whether to indent the first line |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Indented text |
Examples:
Source code in provide/foundation/formatting/text.py
pluralize
¶
Get singular or plural form based on count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Item count |
required |
singular
|
str
|
Singular form |
required |
plural
|
str | None
|
Plural form (default: singular + 's') |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Appropriate singular/plural form with count |
Examples:
>>> pluralize(1, "file")
'1 file'
>>> pluralize(5, "file")
'5 files'
>>> pluralize(2, "child", "children")
'2 children'
Source code in provide/foundation/formatting/text.py
strip_ansi
¶
to_camel_case
¶
Convert text to camelCase or PascalCase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to convert |
required |
upper_first
|
bool
|
Use PascalCase instead of camelCase |
False
|
Returns:
| Type | Description |
|---|---|
str
|
camelCase or PascalCase text |
Examples:
>>> to_camel_case("hello_world")
'helloWorld'
>>> to_camel_case("hello-world", upper_first=True)
'HelloWorld'
Source code in provide/foundation/formatting/case.py
to_kebab_case
¶
Convert text to kebab-case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
kebab-case text |
Examples:
>>> to_kebab_case("HelloWorld")
'hello-world'
>>> to_kebab_case("some_snake_case")
'some-snake-case'
Source code in provide/foundation/formatting/case.py
to_snake_case
¶
Convert text to snake_case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
snake_case text |
Examples:
>>> to_snake_case("HelloWorld")
'hello_world'
>>> to_snake_case("some-kebab-case")
'some_kebab_case'
Source code in provide/foundation/formatting/case.py
truncate
¶
Truncate text to maximum length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to truncate |
required |
max_length
|
int
|
Maximum length including suffix |
required |
suffix
|
str
|
Suffix to append when truncated |
'...'
|
whole_words
|
bool
|
Truncate at word boundaries |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Truncated text |
Examples:
>>> truncate("Hello world", 8)
'Hello...'
>>> truncate("Hello world", 8, whole_words=False)
'Hello...'
Source code in provide/foundation/formatting/text.py
wrap_text
¶
Wrap text to specified width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to wrap |
required |
width
|
int
|
Maximum line width |
80
|
indent_first
|
int
|
Spaces to indent first line |
0
|
indent_rest
|
int
|
Spaces to indent remaining lines |
0
|
Returns:
| Type | Description |
|---|---|
str
|
Wrapped text |