1
0
Fork 0
mirror of https://github.com/SlavikMIPT/tgcloud.git synced 2025-03-09 15:40:14 +00:00

dedupfs added

This commit is contained in:
Вячеслав Баженов 2019-06-14 12:51:59 +03:00
parent 5b4ed0194e
commit 4d66c44764
14 changed files with 2877 additions and 1 deletions

38
dedupfs/my_formats.py Normal file
View file

@ -0,0 +1,38 @@
from math import floor
def format_timespan(seconds): # {{{1
"""
Format a timespan in seconds as a human-readable string.
"""
result = []
units = [('day', 60 * 60 * 24), ('hour', 60 * 60), ('minute', 60), ('second', 1)]
for name, size in units:
if seconds >= size:
count = seconds / size
seconds %= size
result.append('%i %s%s' % (count, name, floor(count) != 1 and 's' or ''))
if result == []:
return 'less than a second'
if len(result) == 1:
return result[0]
else:
return ', '.join(result[:-1]) + ' and ' + result[-1]
def format_size(nbytes):
"""
Format a byte count as a human-readable file size.
"""
return nbytes < 1024 and '%i bytes' % nbytes \
or nbytes < (1024 ** 2) and __round(nbytes, 1024, 'KB') \
or nbytes < (1024 ** 3) and __round(nbytes, 1024 ** 2, 'MB') \
or nbytes < (1024 ** 4) and __round(nbytes, 1024 ** 3, 'GB') \
or __round(nbytes, 1024 ** 4, 'TB')
def __round(nbytes, divisor, suffix):
nbytes = float(nbytes) / divisor
if floor(nbytes) == nbytes:
return str(int(nbytes)) + ' ' + suffix
else:
return '%.2f %s' % (nbytes, suffix)
# vim: sw=2 sw=2 et