-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaggro.py
executable file
·215 lines (166 loc) · 6.43 KB
/
taggro.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
from sys import stdin, stdout, stderr
from collections import defaultdict, OrderedDict
from argparse import ArgumentParser
class Aggregator:
def __init__(self, init_value):
self.value = init_value
def add_value(self, value):
raise NotImplementedError()
def get_value(self, sep):
return self.value
@staticmethod
def numeric_options(args):
number_type = int
strict = False
for a in args:
if a == 'float' or a == 'f':
number_type = float
elif a == 'strict':
strict = True
else:
raise ValueError('unknown aggregator option %s' % args[0])
return (number_type, strict)
@staticmethod
def create(ctor, args):
if ctor == 'ignore' or ctor == '-':
if len(args) != 0:
raise ValueError('unknown aggregator option %s' % args[0])
return Ignore
if ctor == 'first':
if len(args) != 0:
raise ValueError('unknown aggregator option %s' % args[0])
return First
if ctor == 'last':
if len(args) != 0:
raise ValueError('unknown aggregator option %s' % args[0])
return Last
if ctor == 'group':
if len(args) != 0:
raise ValueError('unknown aggregator option %s' % args[0])
return Group
if ctor == 'count':
if len(args) != 0:
raise ValueError('unknown aggregator option %s' % args[0])
return Count
if ctor == 'sum':
number_type, strict = Aggregator.numeric_options(args)
return Sum.Type(number_type, strict)
if ctor == 'mean':
number_type, strict = Aggregator.numeric_options(args)
return Mean.Type(number_type, strict)
if ctor == 'collect' or ctor == 'set':
if len(args) != 0:
raise ValueError('unknown aggregator option %s' % args[0])
return Values
raise ValueError('unknown aggregator %s' % ctor)
@staticmethod
def parse_token(token):
args = token.split(':')
ctor = args.pop(0)
return Aggregator.create(ctor, args)
class Ignore(Aggregator):
def __init__(self):
Aggregator.__init__(self, None)
def add_value(self, value):
pass
class First(Aggregator):
def __init__(self):
Aggregator.__init__(self, None)
def add_value(self, value):
if self.value is None:
self.value = value
class Last(Aggregator):
def __init__(self):
Aggregator.__init__(self, None)
def add_value(self, value):
self.value = value
class Group(First):
def __init__(self):
First.__init__(self)
class Count(Aggregator):
def __init__(self):
Aggregator.__init__(self, 0)
def add_value(self, value):
self.value += 1
class NumericAggregator(Aggregator):
def __init__(self, number_type, strict=False):
Aggregator.__init__(self, number_type(0))
self.number_type = number_type
self.strict = strict
def add_value(self, value):
try:
self.add_number(self.number_type(value))
except ValueError as e:
if self.strict:
raise e
self.add_missing()
def add_number(self, value):
raise NotImplementedError()
def add_missing(self):
raise NotImplementedError()
@classmethod
def Type(cls, number_type, strict=False):
return lambda: cls(number_type, strict)
class Sum(NumericAggregator):
def __init__(self, number_type, strict=False):
NumericAggregator.__init__(self, number_type, strict)
def add_number(self, value):
self.value += value
def add_missing(self):
pass
class Mean(NumericAggregator):
def __init__(self, number_type, strict=False):
NumericAggregator.__init__(self, number_type, strict)
self.count = 0
def add_number(self, value):
self.value += value
self.count += 1
def add_missing(self):
pass
def get_value(self, sep):
return float(self.value) / self.count
class Values(Aggregator):
def __init__(self):
Aggregator.__init__(self, OrderedDict())
def add_value(self, value):
self.value[value] = None
def get_value(self, sep):
return sep.join(str(x) for x in self.value)
class TAggro(ArgumentParser):
def __init__(self):
ArgumentParser.__init__(self, description='aggregate columns in a table')
self.add_argument('aggregators', metavar='AGG', type=str, nargs='+', default=[], help='aggregators')
self.add_argument('-i', '--input', metavar='FILE', type=str, nargs=1, action='append', dest='input', default=[], help='input file')
self.add_argument('-s', '--separator', metavar='CHAR', type=str, action='store', dest='separator', default='\t', help='column separator character (default: tab)')
self.add_argument('-l', '--list-separator', metavar='SEP', type=str, action='store', dest='list_separator', default=', ', help='list separator (default: comma)')
def run(self):
args = self.parse_args()
self.aggregator_types = tuple(Aggregator.parse_token(a) for a in args.aggregators)
self.group_indexes = tuple(i for (i, at) in enumerate(self.aggregator_types) if at is Group)
self.result = defaultdict(lambda: tuple(at() for at in self.aggregator_types))
if len(args.input) == 0:
self.read_file(stdin, args.separator)
else:
for a in args.input:
self.read_filename(a[0], args.separator)
for cols in self.result.values():
stdout.write(args.separator.join(str(a.get_value(args.list_separator)) for a in cols if not isinstance(a, Ignore)))
stdout.write('\n')
def read_line(self, cols):
group = tuple(cols[i] for i in self.group_indexes)
aggregators = self.result[group]
for agg, val in zip(aggregators, cols):
agg.add_value(val)
def read_file(self, f, sep='\t'):
stderr.write('separator: %s\n' % str(sep))
for line in f:
if line[-1] == '\n':
line = line[:-1]
cols = line.split(sep)
self.read_line(cols)
def read_filename(self, filename, sep='\t'):
with open(filename) as f:
self.read_file(f, sep)
if __name__ == '__main__':
TAggro().run()