Generate a human-readable string representing number of bytes. The units B, kB, MB and GB are used. Args: num_bytes: (`int` or None) Number of bytes. include_b: (`bool`) Include the letter B at the end of the unit. Returns: (`str`) A string representing the number of bytes in a
(num_bytes, include_b=False)
| 53 | |
| 54 | |
| 55 | def bytes_to_readable_str(num_bytes, include_b=False): |
| 56 | """Generate a human-readable string representing number of bytes. |
| 57 | |
| 58 | The units B, kB, MB and GB are used. |
| 59 | |
| 60 | Args: |
| 61 | num_bytes: (`int` or None) Number of bytes. |
| 62 | include_b: (`bool`) Include the letter B at the end of the unit. |
| 63 | |
| 64 | Returns: |
| 65 | (`str`) A string representing the number of bytes in a human-readable way, |
| 66 | including a unit at the end. |
| 67 | """ |
| 68 | |
| 69 | if num_bytes is None: |
| 70 | return str(num_bytes) |
| 71 | if num_bytes < 1024: |
| 72 | result = "%d" % num_bytes |
| 73 | elif num_bytes < 1048576: |
| 74 | result = "%.2fk" % (num_bytes / 1024.0) |
| 75 | elif num_bytes < 1073741824: |
| 76 | result = "%.2fM" % (num_bytes / 1048576.0) |
| 77 | else: |
| 78 | result = "%.2fG" % (num_bytes / 1073741824.0) |
| 79 | |
| 80 | if include_b: |
| 81 | result += "B" |
| 82 | return result |
| 83 | |
| 84 | |
| 85 | def time_to_readable_str(value_us, force_time_unit=None): |