hbutils.scale.size

Overview:

Useful utilities for memory size units, such as MB/KB/B.

size_to_bytes

hbutils.scale.size.size_to_bytes(size: Union[int, float, str, bitmath.Byte]) → int[source]
Overview:

Turn any types of memory size to integer value in bytes.

Arguments:
  • size (Union[int, float, str, Byte]): Any types of size information.

Returns:
  • bytes (int): Int formatted size in bytes.

Examples::
>>> from hbutils.scale import size_to_bytes
>>> size_to_bytes(23344)
23344
>>> size_to_bytes('23356 KB')
23356000
>>> size_to_bytes('3.54 GB')
3540000000
>>> size_to_bytes('3.54 GiB')
__main__:1: UserWarning: Float detected in variable in bytes (3801046056.96), rounded integer value (3801046057) is used.
3801046057

size_to_bytes_str

hbutils.scale.size.size_to_bytes_str(size: Union[int, float, str, bitmath.Byte], precision: Optional[int] = None, system='nist') → str[source]
Overview:

Turn any types of memory size to string value in the best unit.

Parameters:
  • size – Any types of size information.

  • precision – Precsion for float values. Default is None which means just show the original float number.

Returns:
  • bytes (int): String formatted size value in the best unit.

Examples::
>>> from hbutils.scale import size_to_bytes_str
>>> size_to_bytes_str(23344)
'22.796875 KiB'
>>> size_to_bytes_str('23356 KB')
'22.274017333984375 MiB'
>>> size_to_bytes_str('3.54 GB')
'3.296881914138794 GiB'
>>> size_to_bytes_str('3.54 GiB')
__main__:1: UserWarning: Float detected in variable in bytes (3801046056.96), rounded integer value (3801046057) is used.
'3.540000000037253 GiB'
>>> size_to_bytes_str('3.54 GB', precision=0)  # use precision
'3 GiB'
>>> size_to_bytes_str('3.54 GB', precision=3)
'3.297 GiB'
>>> size_to_bytes_str('3.54 GB', system='si')  # use GB/MB/KB instead of GiB/MiB/KiB
'3.54 GB'
>>> size_to_bytes_str('3.54 GB', precision=3, system='si')
'3.540 GB'