File size: 1,195 Bytes
bb7f1f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def format_percentage(part, total):
    return int((part / total) * 100)


def format_bytes(bytes_to_format):
    units = ['B', 'KB', 'MB', 'GB', 'TB']
    i = 0
    while bytes_to_format >= 1024 and i < len(units) - 1:
        bytes_to_format /= 1024
        i += 1
    return f"{bytes_to_format:.2f} {units[i]}"


def format_kilobytes(bytes_to_format):
    units = ['KB', 'MB', 'GB', 'TB']
    i = 0
    while bytes_to_format >= 1024 and i < len(units) - 1:
        bytes_to_format /= 1024
        i += 1
    return f"{bytes_to_format:.2f} {units[i]}"


def format_time(seconds):
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    if h > 0:
        return f"{int(h):02d}:{int(m):02d}:{int(s):02d}"
    else:
        return f"{int(m):02d}:{int(s):02d}"


def format_download_speed(speed):
    if speed is None:
        return 'Undefined'

    units = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s']
    unit_index = 0

    while speed >= 1000 and unit_index < len(units) - 1:
        speed /= 1000.0
        unit_index += 1

    return '{:.2f}{}'.format(speed, units[unit_index])


def format_exception(ex):
    return f"{type(ex).__name__}: {str(ex)}".replace('"', '\\"').replace("'", "\\'")