-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproportion_zero.py
executable file
·47 lines (36 loc) · 1.01 KB
/
proportion_zero.py
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
#!/usr/bin/env python
import re
import sys
def proportion_null(handle, block_size=512):
if block_size < 1:
raise ValueError('block_size must be > 0')
null_block = '\0' * block_size
count_null = 0
total = 0
while True:
block = handle.read(block_size)
if not block:
break
total += 1
if block == null_block[:len(block)]:
count_null += 1
if total == 0:
percentage = None
else:
percentage = 100.0 * count_null / total
return {'null': count_null,
'total': total,
'percentage': percentage}
if __name__ == '__main__':
for filename in sys.argv[1:]:
if filename == '-':
f = sys.stdin
else:
f = open(filename, 'r')
data = proportion_null(f)
if data['percentage'] is None:
print '-',
else:
print '%d%%' % round(data['percentage'], 0),
print '%d/%d' % (data['null'], data['total']),
print f.name