Skip to main content

ByteSize

Converts a string representing a number of bytes with units (such as '1KB' or '11.5MiB') into an integer.

Attributes

AttributeTypeDescription
byte_sizesdict[str, float]A mapping of unit suffixes to their corresponding numeric multipliers used for calculating the total number of bytes.
byte_string_patternstr = 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_rere.PatternA compiled regular expression object used for case-insensitive matching of byte strings during validation.

Constructor

Signature

def ByteSize(
value: Union[int, float, str]
)

Parameters

NameTypeDescription
valueUnion[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

NameTypeDescription
decimalbool = FalseIf True, use decimal units (e.g. 1000 bytes per KB). If False, use binary units (e.g. 1024 bytes per KiB).
separatorstr = ''A string used to split the value and unit. Defaults to an empty string ('').

Returns

TypeDescription
strA 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

NameTypeDescription
unitstrThe unit to convert to. Must be one of the supported byte units (e.g., 'MB', 'GiB') or bit units (e.g., 'mbit', 'kibit').

Returns

TypeDescription
floatThe byte size in the new unit.