Coverage for pyguymer3/convert_bytes_to_pretty_bytes.py: 68%

19 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def convert_bytes_to_pretty_bytes( 

5 uglySize : int, 

6 /, 

7) -> tuple[float, str]: 

8 """Convert a value of bytes to a pretty value of bytes with some units 

9 

10 This function reads in a value of bytes and returns the same value expressed 

11 prettily for a human to read with some units. 

12 

13 Parameters 

14 ---------- 

15 uglySize : int 

16 the value of bytes 

17 

18 Returns 

19 ------- 

20 prettySize : float 

21 the value of bytes in *unit* 

22 unit : str 

23 the units of *prettySize* 

24 

25 Notes 

26 ----- 

27 Copyright 2017 Thomas Guymer [1]_ 

28 

29 References 

30 ---------- 

31 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

32 

33 Examples 

34 -------- 

35 >>> pyguymer3.convert_bytes_to_pretty_bytes(1536) 

36 (1.5, 'KiB') 

37 """ 

38 

39 # Convert input to float and set default units ... 

40 prettySize : float = float(uglySize) # [B] 

41 unit : str = "B" 

42 

43 # Convert to useful units ... 

44 if prettySize >= 1024.0: 

45 prettySize /= 1024.0 # [KiB] 

46 unit = "KiB" 

47 if prettySize >= 1024.0: 

48 prettySize /= 1024.0 # [MiB] 

49 unit = "MiB" 

50 if prettySize >= 1024.0: 

51 prettySize /= 1024.0 # [GiB] 

52 unit = "GiB" 

53 if prettySize >= 1024.0: 

54 prettySize /= 1024.0 # [TiB] 

55 unit = "TiB" 

56 if prettySize >= 1024.0: 

57 prettySize /= 1024.0 # [PiB] 

58 unit = "PiB" 

59 

60 # Return answer ... 

61 return prettySize, unit