Skip to content

Instantly share code, notes, and snippets.

@eddieantonio
Created June 8, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieantonio/d60fc91a147d9c30b7ca87a1e8e7dcbc to your computer and use it in GitHub Desktop.
Save eddieantonio/d60fc91a147d9c30b7ca87a1e8e7dcbc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
from math import log
def size2number(blocks):
number = int(blocks) * 512 # bytes
if number == 0:
return 'empty'
magnitude = int(log(number, 2)) // 10
default = lambda x: '%dTiB' % (x // 1024 ** 3)
return {
0: lambda x: '%d' % x,
1: lambda x: '%dKiB' % (x // 1024 ** 1),
2: lambda x: '%dMiB' % (x // 1024 ** 2),
3: lambda x: '%dGiB' % (x // 1024 ** 3),
}.get(magnitude, default)(number)
for line in sys.stdin:
size, rest = line.split(None, 1)
print(size2number(size), rest, sep='\t', end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment