Skip to content

Instantly share code, notes, and snippets.

View ElectroDrome's full-sized avatar

Andreas Potthoff ElectroDrome

View GitHub Profile
@ElectroDrome
ElectroDrome / format_bytes_6.py
Last active April 29, 2024 20:08
Python: Format a number of bytes into a human readable format
def format_bytes(num, suffix="B"):
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
@ElectroDrome
ElectroDrome / format_bytes_5.py
Last active April 29, 2024 20:08
Python: Format a number of bytes into a human readable format
def format_bytes(size, decimal_places=2):
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']:
if size < 1024.0 or unit == 'PiB':
break
size /= 1024.0
return f"{size:.{decimal_places}f} {unit}"
@ElectroDrome
ElectroDrome / format_bytes_4.py
Last active April 29, 2024 20:10
Python: Format a number of bytes into a human readable format (non looping solution)
from math import log
unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 1, 2, 2, 2])
def format_bytes(num):
if num > 1:
exponent = min(int(log(num, 1024)), len(unit_list) - 1)
quotient = float(num) / 1024**exponent
unit, num_decimals = unit_list[exponent]
format_string = '{:.%sf} {}' % (num_decimals)
return format_string.format(quotient, unit)
if num == 0:
@ElectroDrome
ElectroDrome / format_bytes_3.py
Last active April 29, 2024 20:08
Python: Format a number of bytes into a human readable format
from math import log
def format_bytes(n,pow=0,b=1024,u='B',pre=['']+[p+'i'for p in'KMGTPEZY']):
pow,n=min(int(log(max(n*b**pow,1),b)),len(pre)-1),n*b**pow
return "%%.%if %%s%%s"%abs(pow%(-pow-1))%(n/b**float(pow),pre[pow],u)
@ElectroDrome
ElectroDrome / format_bytes_2.py
Last active April 29, 2024 20:10
Python: Format a number of bytes into a human readable format (non looping solution)
def format_bytes(bytes, units=[' bytes','KB','MB','GB','TB', 'PB', 'EB']):
return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:]) if units[1:] else f'{bytes>>10}ZB'
@ElectroDrome
ElectroDrome / format_bytes_1.py
Last active April 29, 2024 20:08
Python: Format a number of bytes into a human readable format (non looping solution)
def format_bytes(num, suffix='B'):
magnitude = int(math.floor(math.log(num, 1024)))
val = num / math.pow(1024, magnitude)
if magnitude > 7:
return '{:.1f}{}{}'.format(val, 'Yi', suffix)
return '{:3.1f}{}{}'.format(val, ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi'][magnitude], suffix)
@ElectroDrome
ElectroDrome / format_bytes_0.py
Last active April 29, 2024 20:08
Python: Format a number of bytes into a human readable format
def format_bytes(n): # Convert a number of bytes into a human readable format
symbols = ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.3f%s' % (value, s) # 3 digits
return "%sB" % n
@ElectroDrome
ElectroDrome / format_bytes_0.php
Last active April 29, 2024 20:09
PHP: Format a number of bytes into a human readable format
// convert bytes format
function format_Bytes($size,$level=0,$precision=2,$base=1024)
{
if ($size === '0' || $size === null) {
return "0 B";
}
else
$unit = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB','YB');
$times = floor(log($size,$base));
return sprintf("%.".$precision."f",$size/pow($base,($times+$level)))." ".$unit[$times+$level];