summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringutil.py
blob: 4bfa5d7e4e16d3578f7fc04bc9008ab87d4fe1a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

def pretty_size(size):
    if size >= 1024*1024*1024:
        return "%.1f GiB" % round(size/1024/1024/1024.0)
    elif size >= 1024*1024:
        return "%.1f MiB" % round(size/1024/1024.0)
    elif size >= 1024:
        return "%.1f KiB" % round(size/1024.0)
    else:
        return "%u B" % size


def format_disk_space(size):
    """ Convert bytes to a human readable format.
        Returns a tuple, for example (20,"MiB") """

    if size >= 1024*1024*1024:
        return ("%.1f"%round(size/1024/1024/1024.0),"GiB")
    elif size >= 1024*1024:
        return ("%.1f"%round(size/1024/1024.0),"MiB")
    elif size >= 1024:
        return ("%.1f"%round(size/1024.0),"KiB")
    else:
        return ("%u"%size,"B")