Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2019 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of puffotter.
6puffotter is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11puffotter is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with puffotter. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
21def byte_string_to_byte_count(byte_string: str) -> int:
22 """
23 Converts a string representing bytes to a number of bytes.
24 For example: "500K" -> 500 000
25 "2.5M" -> 2 500 000
26 "10GB" -> 10 000 000 000
27 "30kb/s" -> 30 000
28 :param byte_string: The string to convert
29 :return: The amount of bytes
30 """
31 byte_string = byte_string.lower()
33 units = {
34 denominator: 10**(3 * (i + 1))
35 for i, denominator in enumerate([
36 "k", "m", "g", "t", "p", "e"
37 ])
38 }
40 for unit in units:
41 byte_string = byte_string.replace(unit + "b/s", unit)
42 byte_string = byte_string.replace(unit + "b", unit)
44 multiplier = 1
45 byte_num = ""
46 for i, char in enumerate(byte_string):
47 if char.isdigit() or char == ".":
48 byte_num += char
49 else:
50 # Unit should be last symbol in string
51 if len(byte_string) - 1 != i: 51 ↛ 52line 51 didn't jump to line 52, because the condition on line 51 was never true
52 raise ValueError()
53 else:
54 try:
55 multiplier = units[char]
56 except KeyError:
57 raise ValueError()
59 byte_count = int(multiplier * float(byte_num))
60 return byte_count
63def human_readable_bytes(
64 bytecount: int,
65 base_1024: bool = False,
66 remove_trailing_zeroes: bool = True
67) -> str:
68 """
69 Converts an amount of bytes into a human-readable string
70 :param bytecount: The bytes to convert
71 :param base_1024: Whether or not to use 1024 as base (for mebibytes etc)
72 :param remove_trailing_zeroes: If set to True, will remove any trailing
73 zeroes from the string
74 :return: The human-readable string
75 """
76 negative = False
77 if bytecount < 0: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true
78 negative = True
79 bytecount *= -1
81 units = ["K", "M", "G", "T", "P", "E", "Z", "Y"]
82 unit_index = -1
83 base = 1024 if base_1024 else 1000
84 _bytes = float(bytecount)
86 while True:
87 _bytes /= base
88 unit_index += 1
90 if int(_bytes) < base or unit_index == len(units) - 1:
91 break
93 # Formatting
94 bytestring = ("%.3f" % _bytes)
96 if remove_trailing_zeroes: 96 ↛ 105line 96 didn't jump to line 105, because the condition on line 96 was never false
97 string_index = len(bytestring) - 1
98 while bytestring[string_index] == "0":
99 string_index -= 1
100 bytestring = bytestring[0:string_index + 1]
102 if bytestring.endswith("."):
103 bytestring = bytestring[0:-1]
105 i = "i" if base_1024 else ""
106 bytestring += units[unit_index] + i + "B"
108 if negative: 108 ↛ 109line 108 didn't jump to line 109, because the condition on line 108 was never true
109 bytestring = "-" + bytestring
110 return bytestring