forked from pixelb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman.py
More file actions
executable file
·106 lines (94 loc) · 2.81 KB
/
human.py
File metadata and controls
executable file
·106 lines (94 loc) · 2.81 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# Convert a number for human consumption
# License: LGPLv2
# Author:
# P@draigBrady.com
# Changes:
# V1.0 09 Jan 2006 Initial release
#TODO: support ranges for --column option
#TODO: support converting from back from "human" numbers to "standard" numbers
#TODO: support aligning output like `column -t`
#TODO: support --col-delimiters option
# Divisor can be 1, 1000, 1024
#
# A divisor of 1 => the thousands separator
# appropriate to ones locale is inserted.
# So the locale must be set before this
# functionality is used (see below).
#
# With other divisors the output is aligned
# in a 7 or 8 character column respectively,
# which one can strip() if the display is not
# using a fixed width font.
def human_num(num, divisor=1, power=""):
num=float(num)
if divisor == 1:
return locale.format("%ld",int(num),1)
elif divisor == 1000:
powers=[" ","K","M","G","T","P"]
elif divisor == 1024:
powers=[" ","Ki","Mi","Gi","Ti","Pi"]
else:
raise ValueError, "Invalid divisor"
if not power: power=powers[0]
while num >= 1000: #4 digits
num /= divisor
power=powers[powers.index(power)+1]
human_num(num,divisor,power)
if power.strip():
return "%6.1f%s" % (num,power)
else:
return "%4ld %s" % (num,power)
import locale
locale.setlocale(locale.LC_ALL,'')
import os
import sys
import getopt
def Usage():
print "Usage: %s [OPTION] [PATH]" % os.path.split(sys.argv[0])[1]
print " --divisor=value The default value is 1 which means insert thousands separator."
print " Other possible values are 1000 and 1024."
print " --columns=1,2,3"
print " --help display help"
try:
lOpts, lArgs = getopt.getopt(sys.argv[1:], "", ["help","divisor=","columns="])
if len(lArgs) == 0:
infile = sys.stdin
elif len(lArgs) == 1:
infile = file(lArgs[0])
else:
Usage()
sys.exit(None)
if ("--help","") in lOpts:
Usage()
sys.exit(None)
divisor=1
columns=[]
for opt in lOpts:
if opt[0] == "--divisor":
divisor=opt[1]
if divisor == "1":
divisor = 1
elif divisor=="1000" or divisor=="1024":
divisor = float(divisor)
else:
raise getopt.error, "Invalid divisor"
if opt[0] == "--columns":
columns=[int(col) for col in opt[1].split(",")]
except getopt.error, msg:
print msg
print
Usage()
sys.exit(2)
for line in infile:
line = line.split()
column=0
for str in line:
column+=1
if not len(columns) or column in columns:
try:
str = human_num(str,divisor)
except:
pass
print str,
print