ByteSize
Converts a string representing a number of bytes with units (such as '1KB' or '11.5MiB') into an integer.
Attributes
| Attribute | Type | Description |
|---|---|---|
| byte_sizes | dict[str, float] | A mapping of unit suffixes to their corresponding numeric multipliers used for calculating the total number of bytes. |
| byte_string_pattern | str = r'^\s*(\d*.?\d+)\s*(\w+)?' | A regex pattern used to extract the numeric scalar and the unit suffix from a byte string representation. |
| byte_string_re | re.Pattern | A compiled regular expression object used for case-insensitive matching of byte strings during validation. |
Constructor
Signature
def ByteSize(
value: Union[int, float, str]
)
Parameters
| Name | Type | Description |
|---|---|---|
| value | Union[int, float, str] | The value representing the number of bytes. While not explicitly defined in an init override, as an int subclass, it typically accepts numeric values or strings that can be cast to an integer. |
Methods
human_readable()
@classmethod
def human_readable(
decimal: bool = False,
separator: str = ''
) - > str
Converts a byte size to a human readable string.
Parameters
| Name | Type | Description |
|---|---|---|
| decimal | bool = False | If True, use decimal units (e.g. 1000 bytes per KB). If False, use binary units (e.g. 1024 bytes per KiB). |
| separator | str = '' | A string used to split the value and unit. Defaults to an empty string (''). |
Returns
| Type | Description |
|---|---|
str | A human readable string representation of the byte size. |
to()
@classmethod
def to(
unit: str
) - > float
Converts a byte size to another unit, including both byte and bit units.
Parameters
| Name | Type | Description |
|---|---|---|
| unit | str | The unit to convert to. Must be one of the supported byte units (e.g., 'MB', 'GiB') or bit units (e.g., 'mbit', 'kibit'). |
Returns
| Type | Description |
|---|---|
float | The byte size in the new unit. |