-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
executable file
·54 lines (38 loc) · 1.12 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
def dict_from_kw_args(args):
""" Return a dictionary based on ['key=val'] entries
>>> dict_from_kw_args(['x=10', 'y=foo'])
{'x': '10', 'y': 'foo'}
"""
if args:
return dict(i.split('=', maxsplit=1) for i in args)
else:
return {}
def perc_diff(v1, v2):
""" Return the difference between two number as percentage
>>> perc_diff(10, 15)
40.0
>>> perc_diff(0, 0)
0.0
"""
try:
return (abs(v1 - v2) / ((v1 + v2) / 2.0)) * 100
except ZeroDivisionError:
return 0.0
def human_readable_byte_size(value):
""" Turn value into a (file_size, byte_size_unit) tuple.
`file_size` is `value` adjusted to match the unit.
>>> human_readable_byte_size(1024 * 17)
(17.0, 'KB')
"""
for count in ('Bytes', 'KB', 'MB', 'GB'):
if value < 1024.0:
return (value, count)
value /= 1024.0
return (value, 'TB')
def format_byte_size(value):
file_size, unit = human_readable_byte_size(value)
return f'{file_size:8.2f} {unit}'
if __name__ == '__main__':
import doctest
doctest.testmod()