filesize.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """Functions for reporting filesizes. Borrowed from https://github.com/PyFilesystem/pyfilesystem2
  2. The functions declared in this module should cover the different
  3. use cases needed to generate a string representation of a file size
  4. using several different units. Since there are many standards regarding
  5. file size units, three different functions have been implemented.
  6. See Also:
  7. * `Wikipedia: Binary prefix <https://en.wikipedia.org/wiki/Binary_prefix>`_
  8. """
  9. __all__ = ["decimal"]
  10. from typing import Iterable, List, Optional, Tuple
  11. def _to_str(
  12. size: int,
  13. suffixes: Iterable[str],
  14. base: int,
  15. *,
  16. precision: Optional[int] = 1,
  17. separator: Optional[str] = " ",
  18. ) -> str:
  19. if size == 1:
  20. return "1 byte"
  21. elif size < base:
  22. return f"{size:,} bytes"
  23. for i, suffix in enumerate(suffixes, 2): # noqa: B007
  24. unit = base**i
  25. if size < unit:
  26. break
  27. return "{:,.{precision}f}{separator}{}".format(
  28. (base * size / unit),
  29. suffix,
  30. precision=precision,
  31. separator=separator,
  32. )
  33. def pick_unit_and_suffix(size: int, suffixes: List[str], base: int) -> Tuple[int, str]:
  34. """Pick a suffix and base for the given size."""
  35. for i, suffix in enumerate(suffixes):
  36. unit = base**i
  37. if size < unit * base:
  38. break
  39. return unit, suffix
  40. def decimal(
  41. size: int,
  42. *,
  43. precision: Optional[int] = 1,
  44. separator: Optional[str] = " ",
  45. ) -> str:
  46. """Convert a filesize in to a string (powers of 1000, SI prefixes).
  47. In this convention, ``1000 B = 1 kB``.
  48. This is typically the format used to advertise the storage
  49. capacity of USB flash drives and the like (*256 MB* meaning
  50. actually a storage capacity of more than *256 000 000 B*),
  51. or used by **Mac OS X** since v10.6 to report file sizes.
  52. Arguments:
  53. int (size): A file size.
  54. int (precision): The number of decimal places to include (default = 1).
  55. str (separator): The string to separate the value from the units (default = " ").
  56. Returns:
  57. `str`: A string containing a abbreviated file size and units.
  58. Example:
  59. >>> filesize.decimal(30000)
  60. '30.0 kB'
  61. >>> filesize.decimal(30000, precision=2, separator="")
  62. '30.00kB'
  63. """
  64. return _to_str(
  65. size,
  66. ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
  67. 1000,
  68. precision=precision,
  69. separator=separator,
  70. )