-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.py
executable file
·34 lines (30 loc) · 1.18 KB
/
segment.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
#!/usr/bin/python3
"""Segments a sample stream into non-overlapping equal-sized windows."""
import sys
import argparse
def main(args, prog=None):
p = argparse.ArgumentParser(prog=prog)
p.add_argument('-t', metavar='SIZE', dest='window_size',
type=int, default=63,
help='window size (default: 63)')
p.add_argument('-o', metavar='OUTPUT', dest='output',
type=argparse.FileType('a'), default=sys.stdout,
help='output stream (default: stdout)')
p.add_argument('input', nargs='?', type=argparse.FileType('r'),
default=sys.stdin,
help='input stream (default: stdin)')
args = p.parse_args(args)
window = []
for line in args.input:
if line.startswith('#'):
window.clear()
else:
window.append(line.strip())
if len(window) >= args.window_size:
for window_line in window:
print(window_line, file=args.output)
print('# ---', file=args.output)
args.output.flush()
window.clear()
if __name__ == '__main__':
main(sys.argv[1:], sys.argv[0])