-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman
More file actions
executable file
·29 lines (26 loc) · 1.05 KB
/
human
File metadata and controls
executable file
·29 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python2.7
import argparse
from math import log
from sys import stdin
parser=argparse.ArgumentParser(description="Convert bytes to human-readable format")
outfmt=parser.add_mutually_exclusive_group()
outfmt.add_argument("-b", "--binary", action="store_true", help="Use binary (1024-based) units")
outfmt.add_argument("-d", "--decimal", action="store_true", help="Use decimal (1000-based) units")
parser.add_argument("--reverse", action="store_true", help="Unimplemented. Convert human readable number to bytes")
args = parser.parse_args()
if not args.binary and not args.decimal:
args.binary=True
dec_pref = ['', 'K', 'M', 'G', 'T', 'P']
def convert(size, decimal=False):
if decimal:
prefixes = dec_pref
base = 1000
else:
prefixes = [''] + [p+'i' for p in dec_pref[1:]]
base = 2**10
magnitude = int(log(size, base))
coef = 1.0 * size / base**magnitude
decpts = str(1 if coef < 10.0 else 0)
return ("{0:."+decpts+"f} {1}B").format(coef,prefixes[magnitude])
for line in stdin.readlines():
print convert(long(line.strip()), decimal=args.decimal)