tests.test_backup_compress.humanize_bytes()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
import os
2
import shutil
3
from dirutility.backup import ZipBackup
4
5
directory = 'data/games'
6
destination = os.path.join(os.path.dirname(__file__), 'data')
7
if os.path.exists(destination):
8
    shutil.rmtree(destination)
9
if not os.path.exists(destination):
10
    os.mkdir(destination)
11
12
13
def humanize_bytes(bytes, precision=2):
14
    """Return a humanized string representation of a number of bytes."""
15
    abbrevs = ((1 << 50, 'PB'), (1 << 40, 'TB'), (1 << 30, 'GB'), (1 << 20, 'MB'), (1 << 10, 'kB'), (1, 'bytes'))
16
    if bytes == 1:
17
        return '1 byte'
18
    for factor, suffix in abbrevs:
19
        if bytes >= factor:
20
            break
21
    return '%.*f %s' % (precision, bytes / factor, suffix)
0 ignored issues
show
introduced by
The variable factor does not seem to be defined in case the for loop on line 18 is not entered. Are you sure this can never be the case?
Loading history...
introduced by
The variable suffix does not seem to be defined in case the for loop on line 18 is not entered. Are you sure this can never be the case?
Loading history...
22
23
24
for i in range(0, 10):
25
    d = ZipBackup(directory, destination, compress_level=i).backup()
26
    print(humanize_bytes(os.path.getsize(str(d))), d)
27