reddit-image-wall-getter/reddit_imgs/system/format_file_size.py

19 lines
581 B
Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from typing import Union
Numeric = Union[int, float]
def format_power10(count: Numeric, intervals=1000, cutoff=2000, labels=',k,M,G,T,P,E,Z,Y'.split(','), suffix='B') -> str:
times_cut = 0
while count > cutoff:
count /= intervals
times_cut += 1
return '%.1f %s%s' % (count, labels[times_cut], suffix)
def format_power2(count: Numeric, intervals=1024, cutoff=2048, labels=',ki,Mi,Gi,Ti,Pi,Ei,Zi,Yi'.split(','), suffix='B') -> str:
return format_power10(count, intervals, cutoff, labels, suffix)