Utility functions¤
asab.utils
provides several helper functions for manipulation with URLs, time conversion etc.
asab.utils
¤
convert_to_bytes(size)
¤
Convert a size string to bytes. The size string should be a number optionally followed by a unit (B, kB, MB, GB, or TB), e.g., "10MB".
Examples:
Configuration:
Usage:Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Size string. |
required |
Returns:
Type | Description |
---|---|
int
|
Size in bytes. |
Raises:
Type | Description |
---|---|
ValueError
|
If the size string does not have the correct format. |
Source code in asab/utils.py
convert_to_seconds(value)
¤
Parse time duration string (e.g. "3h", "20m" or "1y") and convert it into seconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
Time duration string. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Number of seconds. |
Raises:
Type | Description |
---|---|
ValueError
|
If the string is not in a valid format. |
Source code in asab/utils.py
running_in_container()
¤
Check if the application is running in Docker or Podman container.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
Source code in asab/utils.py
string_to_boolean(value)
¤
Convert common boolean string values (e.g. 'yes' or 'no') into boolean.
True
:1
,'yes'
,'true'
,'on'
False
:0
,'no'
,'false'
,'off'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
A value to be parsed. |
required |
Returns:
Type | Description |
---|---|
bool
|
Value converted to bool. |
Source code in asab/utils.py
validate_url(input_url, scheme)
¤
Parse URL, remove leading and trailing whitespaces and a trailing slash.
If scheme
is specified, check if it matches the input_url
scheme.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_url
|
str
|
URL to be parsed and validated. |
required |
scheme
|
str | tuple[str] | None
|
Requested URL schema. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Parsed and validated URL. |