Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert all sizes to terabytes pandas

def convertFloatToDecimal(f=0.0, precision=2):
    '''
    Convert a float to string of decimal.
    precision: by default 2.
    If no arg provided, return "0.00".
    '''
    return ("%." + str(precision) + "f") % f

def formatFileSize(size, sizeIn, sizeOut, precision=0):
    '''
    Convert file size to a string representing its value in B, KB, MB and GB.
    The convention is based on sizeIn as original unit and sizeOut
    as final unit. 
    '''
    assert sizeIn.upper() in {"B", "KB", "MB", "GB"}, "sizeIn type error"
    assert sizeOut.upper() in {"B", "KB", "MB", "GB"}, "sizeOut type error"
    if sizeIn == "B":
        if sizeOut == "KB":
            return convertFloatToDecimal((size/1024.0), precision)
        elif sizeOut == "MB":
            return convertFloatToDecimal((size/1024.0**2), precision)
        elif sizeOut == "GB":
            return convertFloatToDecimal((size/1024.0**3), precision)
    elif sizeIn == "KB":
        if sizeOut == "B":
            return convertFloatToDecimal((size*1024.0), precision)
        elif sizeOut == "MB":
            return convertFloatToDecimal((size/1024.0), precision)
        elif sizeOut == "GB":
            return convertFloatToDecimal((size/1024.0**2), precision)
    elif sizeIn == "MB":
        if sizeOut == "B":
            return convertFloatToDecimal((size*1024.0**2), precision)
        elif sizeOut == "KB":
            return convertFloatToDecimal((size*1024.0), precision)
        elif sizeOut == "GB":
            return convertFloatToDecimal((size/1024.0), precision)
    elif sizeIn == "GB":
        if sizeOut == "B":
            return convertFloatToDecimal((size*1024.0**3), precision)
        elif sizeOut == "KB":
            return convertFloatToDecimal((size*1024.0**2), precision)
        elif sizeOut == "MB":
            return convertFloatToDecimal((size*1024.0), precision)
Comment

PREVIOUS NEXT
Code Example
Python :: python - match two df on a variable with different name 
Python :: python use getcontext 
Python :: remove duplicates in json python 
Python :: matplotlib tick label position left and right x axis 
Python :: Common Python String Methods 
Python :: How to take n space separated Integer in a list in python? 
Python :: create a flask app 
Python :: or en python 
Python :: how to define number in python 
Python :: cpickle python 
Python :: plt.semilogx 
Python :: pass python 
Python :: python melt 
Python :: python docker 
Python :: use get method request html page python 
Python :: python different types of loops 
Python :: Python Generators with a Loop 
Python :: python code for create diamond shape with integer 
Python :: how to draw dendrogram in python 
Python :: pytest create server 
Python :: float python 
Python :: pandas replace word begins with contains 
Python :: python variables 
Python :: upper python python.org 
Python :: pytest fixtures scope explained 
Python :: How to Replace substrings in python 
Python :: get midnight of current day python 
Python :: python classes and objects 
Python :: seaborn stripplot min max 
Python :: how to register a model in django 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =