-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapcity_sum.py
More file actions
executable file
·56 lines (49 loc) · 1.64 KB
/
capcity_sum.py
File metadata and controls
executable file
·56 lines (49 loc) · 1.64 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# coding=utf-8
import os
import sys
import fileinput
SUPPORT_POSTFIX = ["K", "M", "G", "T", "P"]
KB_VALUE = 1024
MB_VALUE = 1024 * 1024
GB_VALUE = 1024 * 1024 * 1024
TB_VALUE = 1024 * 1024 * 1024 * 1024
PB_VALUE = 1024 * 1024 * 1024 * 1024 * 1024
def convertToByte(unit):
byteValue = 0;
postFix = unit[-1:].upper()
rawValue = float(unit[:-1])
if not postFix in SUPPORT_POSTFIX:
print "unsupport unit", unit, "skip it!"
if postFix == "K":
byteValue = rawValue * KB_VALUE
elif postFix == "M":
byteValue = rawValue * MB_VALUE
elif postFix == "G":
byteValue = rawValue * GB_VALUE
elif postFix == "T":
byteValue = rawValue * TB_VALUE
elif postFix == "P":
byteValue = rawValue * PB_VALUE
return byteValue
if __name__ == "__main__":
byteValues = []
# see more stdin on http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python
# convert each
for line in fileinput.input():
# strip trailing spaces
line = line.strip()
# print the processing data
print "Processing ", line
byteValue = convertToByte(line)
byteValues.append(byteValue)
# calculate total
totalBytes = reduce(lambda x,y: x + y, byteValues)
print "Total bytes is {totalBytes} bytes, or {totalKB} KB, or {totalMB} MB, or {totalGB} GB, or {totalTB} TB, or {totalPB} PB".format(
totalBytes=totalBytes,
totalKB=totalBytes / MB_VALUE,
totalMB=totalBytes / MB_VALUE,
totalGB=totalBytes / GB_VALUE,
totalTB=totalBytes / TB_VALUE,
totalPB=totalBytes / PB_VALUE
)