mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 10:42:34 +01:00
implement 'util.format_value()'
This commit is contained in:
parent
552032d4fb
commit
2792ed6e4b
@ -1,5 +1,7 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
## 1.18.2 - 2021-07-23
|
## 1.18.2 - 2021-07-23
|
||||||
### Additions
|
### Additions
|
||||||
- [bbc] add `gallery` and `programme` extractors ([#1706](https://github.com/mikf/gallery-dl/issues/1706))
|
- [bbc] add `gallery` and `programme` extractors ([#1706](https://github.com/mikf/gallery-dl/issues/1706))
|
||||||
|
@ -97,6 +97,17 @@ def generate_token(size=16):
|
|||||||
return binascii.hexlify(data).decode()
|
return binascii.hexlify(data).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def format_value(value, unit="B", suffixes="kMGTPEZY"):
|
||||||
|
value = format(value)
|
||||||
|
value_len = len(value)
|
||||||
|
index = value_len - 4
|
||||||
|
if index >= 0:
|
||||||
|
offset = (value_len - 1) % 3 + 1
|
||||||
|
return (value[:offset] + "." + value[offset:offset+2] +
|
||||||
|
suffixes[index // 3] + unit)
|
||||||
|
return value + unit
|
||||||
|
|
||||||
|
|
||||||
def combine_dict(a, b):
|
def combine_dict(a, b):
|
||||||
"""Recursively combine the contents of 'b' into 'a'"""
|
"""Recursively combine the contents of 'b' into 'a'"""
|
||||||
for key, value in b.items():
|
for key, value in b.items():
|
||||||
|
@ -6,4 +6,4 @@
|
|||||||
# it under the terms of the GNU General Public License version 2 as
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
# published by the Free Software Foundation.
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
__version__ = "1.18.2"
|
__version__ = "1.18.3-dev"
|
||||||
|
@ -532,6 +532,22 @@ class TestOther(unittest.TestCase):
|
|||||||
self.assertEqual(len(token), 80 * 2)
|
self.assertEqual(len(token), 80 * 2)
|
||||||
self.assertRegex(token, r"^[0-9a-f]+$")
|
self.assertRegex(token, r"^[0-9a-f]+$")
|
||||||
|
|
||||||
|
def test_format_value(self):
|
||||||
|
self.assertEqual(util.format_value(0) , "0B")
|
||||||
|
self.assertEqual(util.format_value(1) , "1B")
|
||||||
|
self.assertEqual(util.format_value(12) , "12B")
|
||||||
|
self.assertEqual(util.format_value(123) , "123B")
|
||||||
|
self.assertEqual(util.format_value(1234) , "1.23kB")
|
||||||
|
self.assertEqual(util.format_value(12345) , "12.34kB")
|
||||||
|
self.assertEqual(util.format_value(123456) , "123.45kB")
|
||||||
|
self.assertEqual(util.format_value(1234567) , "1.23MB")
|
||||||
|
self.assertEqual(util.format_value(12345678) , "12.34MB")
|
||||||
|
self.assertEqual(util.format_value(123456789) , "123.45MB")
|
||||||
|
self.assertEqual(util.format_value(1234567890), "1.23GB")
|
||||||
|
|
||||||
|
self.assertEqual(util.format_value(123 , "B/s"), "123B/s")
|
||||||
|
self.assertEqual(util.format_value(123456, "B/s"), "123.45kB/s")
|
||||||
|
|
||||||
def test_combine_dict(self):
|
def test_combine_dict(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
util.combine_dict({}, {}),
|
util.combine_dict({}, {}),
|
||||||
|
Loading…
Reference in New Issue
Block a user