convert all sizes to terabytes pandas

Solutions on MaxInterview for convert all sizes to terabytes pandas by the best coders in the world

showing results for - "convert all sizes to terabytes pandas"
Diego Alejandro
13 Oct 2020
1def convertFloatToDecimal(f=0.0, precision=2):
2    '''
3    Convert a float to string of decimal.
4    precision: by default 2.
5    If no arg provided, return "0.00".
6    '''
7    return ("%." + str(precision) + "f") % f
8
9def formatFileSize(size, sizeIn, sizeOut, precision=0):
10    '''
11    Convert file size to a string representing its value in B, KB, MB and GB.
12    The convention is based on sizeIn as original unit and sizeOut
13    as final unit. 
14    '''
15    assert sizeIn.upper() in {"B", "KB", "MB", "GB"}, "sizeIn type error"
16    assert sizeOut.upper() in {"B", "KB", "MB", "GB"}, "sizeOut type error"
17    if sizeIn == "B":
18        if sizeOut == "KB":
19            return convertFloatToDecimal((size/1024.0), precision)
20        elif sizeOut == "MB":
21            return convertFloatToDecimal((size/1024.0**2), precision)
22        elif sizeOut == "GB":
23            return convertFloatToDecimal((size/1024.0**3), precision)
24    elif sizeIn == "KB":
25        if sizeOut == "B":
26            return convertFloatToDecimal((size*1024.0), precision)
27        elif sizeOut == "MB":
28            return convertFloatToDecimal((size/1024.0), precision)
29        elif sizeOut == "GB":
30            return convertFloatToDecimal((size/1024.0**2), precision)
31    elif sizeIn == "MB":
32        if sizeOut == "B":
33            return convertFloatToDecimal((size*1024.0**2), precision)
34        elif sizeOut == "KB":
35            return convertFloatToDecimal((size*1024.0), precision)
36        elif sizeOut == "GB":
37            return convertFloatToDecimal((size/1024.0), precision)
38    elif sizeIn == "GB":
39        if sizeOut == "B":
40            return convertFloatToDecimal((size*1024.0**3), precision)
41        elif sizeOut == "KB":
42            return convertFloatToDecimal((size*1024.0**2), precision)
43        elif sizeOut == "MB":
44            return convertFloatToDecimal((size*1024.0), precision)
45