-
Notifications
You must be signed in to change notification settings - Fork 191
/
test_reductions.py
329 lines (260 loc) · 9.26 KB
/
test_reductions.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from __future__ import absolute_import, print_function, division
import operator
from collections import OrderedDict
from petl.test.helpers import ieq
from petl.util import strjoin
from petl.transform.reductions import rowreduce, aggregate, \
mergeduplicates, Conflict, fold
def test_rowreduce():
table1 = (('foo', 'bar'),
('a', 3),
('a', 7),
('b', 2),
('b', 1),
('b', 9),
('c', 4))
def sumbar(key, rows):
return [key, sum(row[1] for row in rows)]
table2 = rowreduce(table1, key='foo', reducer=sumbar,
header=['foo', 'barsum'])
expect2 = (('foo', 'barsum'),
('a', 10),
('b', 12),
('c', 4))
ieq(expect2, table2)
def test_rowreduce_fieldnameaccess():
table1 = (('foo', 'bar'),
('a', 3),
('a', 7),
('b', 2),
('b', 1),
('b', 9),
('c', 4))
def sumbar(key, records):
return [key, sum([rec['bar'] for rec in records])]
table2 = rowreduce(table1, key='foo', reducer=sumbar,
header=['foo', 'barsum'])
expect2 = (('foo', 'barsum'),
('a', 10),
('b', 12),
('c', 4))
ieq(expect2, table2)
def test_rowreduce_more():
table1 = (('foo', 'bar'),
('aa', 3),
('aa', 7),
('bb', 2),
('bb', 1),
('bb', 9),
('cc', 4))
def sumbar(key, records):
return [key, sum(rec['bar'] for rec in records)]
table2 = rowreduce(table1, key='foo', reducer=sumbar,
header=['foo', 'barsum'])
expect2 = (('foo', 'barsum'),
('aa', 10),
('bb', 12),
('cc', 4))
ieq(expect2, table2)
def test_rowreduce_empty():
table = (('foo', 'bar'),)
expect = (('foo', 'bar'),)
reducer = lambda key, rows: (key, [r[0] for r in rows])
actual = rowreduce(table, key='foo', reducer=reducer,
header=('foo', 'bar'))
ieq(expect, actual)
def test_aggregate_simple():
table1 = (('foo', 'bar', 'baz'),
('a', 3, True),
('a', 7, False),
('b', 2, True),
('b', 2, False),
('b', 9, False),
('c', 4, True))
# simplest signature - aggregate whole rows
table2 = aggregate(table1, 'foo', len)
expect2 = (('foo', 'value'),
('a', 2),
('b', 3),
('c', 1))
ieq(expect2, table2)
ieq(expect2, table2)
# next simplest signature - aggregate single field
table3 = aggregate(table1, 'foo', sum, 'bar')
expect3 = (('foo', 'value'),
('a', 10),
('b', 13),
('c', 4))
ieq(expect3, table3)
ieq(expect3, table3)
# alternative signature for simple aggregation
table4 = aggregate(table1, key=('foo', 'bar'), aggregation=list,
value=('bar', 'baz'))
expect4 = (('foo', 'bar', 'value'),
('a', 3, [(3, True)]),
('a', 7, [(7, False)]),
('b', 2, [(2, True), (2, False)]),
('b', 9, [(9, False)]),
('c', 4, [(4, True)]))
ieq(expect4, table4)
ieq(expect4, table4)
def test_aggregate_multifield():
table1 = (('foo', 'bar'),
('a', 3),
('a', 7),
('b', 2),
('b', 1),
('b', 9),
('c', 4))
# dict arg
aggregators = OrderedDict()
aggregators['count'] = len
aggregators['minbar'] = 'bar', min
aggregators['maxbar'] = 'bar', max
aggregators['sumbar'] = 'bar', sum
aggregators['listbar'] = 'bar', list
aggregators['bars'] = 'bar', strjoin(', ')
table2 = aggregate(table1, 'foo', aggregators)
expect2 = (('foo', 'count', 'minbar', 'maxbar', 'sumbar', 'listbar',
'bars'),
('a', 2, 3, 7, 10, [3, 7], '3, 7'),
('b', 3, 1, 9, 12, [2, 1, 9], '2, 1, 9'),
('c', 1, 4, 4, 4, [4], '4'))
ieq(expect2, table2)
ieq(expect2, table2) # check can iterate twice
# use suffix notation
table3 = aggregate(table1, 'foo')
table3['count'] = len
table3['minbar'] = 'bar', min
table3['maxbar'] = 'bar', max
table3['sumbar'] = 'bar', sum
table3['listbar'] = 'bar' # default aggregation is list
table3['bars'] = 'bar', strjoin(', ')
ieq(expect2, table3)
# list arg
aggregators = [('count', len),
('minbar', 'bar', min),
('maxbar', 'bar', max),
('sumbar', 'bar', sum),
('listbar', 'bar', list),
('bars', 'bar', strjoin(', '))]
table4 = aggregate(table1, 'foo', aggregators)
ieq(expect2, table4)
ieq(expect2, table4) # check can iterate twice
def test_aggregate_more():
table1 = (('foo', 'bar'),
('aa', 3),
('aa', 7),
('bb', 2),
('bb', 1),
('bb', 9),
('cc', 4),
('dd', 3))
aggregators = OrderedDict()
aggregators['minbar'] = 'bar', min
aggregators['maxbar'] = 'bar', max
aggregators['sumbar'] = 'bar', sum
aggregators['listbar'] = 'bar' # default aggregation is list
aggregators['bars'] = 'bar', strjoin(', ')
table2 = aggregate(table1, 'foo', aggregators)
expect2 = (('foo', 'minbar', 'maxbar', 'sumbar', 'listbar', 'bars'),
('aa', 3, 7, 10, [3, 7], '3, 7'),
('bb', 1, 9, 12, [2, 1, 9], '2, 1, 9'),
('cc', 4, 4, 4, [4], '4'),
('dd', 3, 3, 3, [3], '3'))
ieq(expect2, table2)
ieq(expect2, table2) # check can iterate twice
table3 = aggregate(table1, 'foo')
table3['minbar'] = 'bar', min
table3['maxbar'] = 'bar', max
table3['sumbar'] = 'bar', sum
table3['listbar'] = 'bar' # default aggregation is list
table3['bars'] = 'bar', strjoin(', ')
ieq(expect2, table3)
def test_aggregate_multiple_source_fields():
table = (('foo', 'bar', 'baz'),
('a', 3, True),
('a', 7, False),
('b', 2, True),
('b', 2, False),
('b', 9, False),
('c', 4, True))
expect = (('foo', 'bar', 'value'),
('a', 3, [(3, True)]),
('a', 7, [(7, False)]),
('b', 2, [(2, True), (2, False)]),
('b', 9, [(9, False)]),
('c', 4, [(4, True)]))
actual = aggregate(table, ('foo', 'bar'), list, ('bar', 'baz'))
ieq(expect, actual)
ieq(expect, actual)
actual = aggregate(table, key=('foo', 'bar'), aggregation=list,
value=('bar', 'baz'))
ieq(expect, actual)
ieq(expect, actual)
actual = aggregate(table, key=('foo', 'bar'))
actual['value'] = ('bar', 'baz'), list
ieq(expect, actual)
ieq(expect, actual)
def test_aggregate_empty():
table = (('foo', 'bar'),)
aggregators = OrderedDict()
aggregators['minbar'] = 'bar', min
aggregators['maxbar'] = 'bar', max
aggregators['sumbar'] = 'bar', sum
actual = aggregate(table, 'foo', aggregators)
expect = (('foo', 'minbar', 'maxbar', 'sumbar'),)
ieq(expect, actual)
def test_mergeduplicates():
table = (('foo', 'bar', 'baz'),
('A', 1, 2),
('B', '2', None),
('D', 'xyz', 9.4),
('B', None, u'7.8', True),
('E', None, 42.),
('D', 'xyz', 12.3),
('A', 2, None))
# value overrides missing
result = mergeduplicates(table, 'foo', missing=None)
expectation = (('foo', 'bar', 'baz'),
('A', Conflict([1, 2]), 2),
('B', '2', u'7.8'),
('D', 'xyz', Conflict([9.4, 12.3])),
('E', None, 42.))
ieq(expectation, result)
def test_mergeduplicates_empty():
table = (('foo', 'bar'),)
expect = (('foo', 'bar'),)
actual = mergeduplicates(table, key='foo')
ieq(expect, actual)
def test_mergeduplicates_shortrows():
table = [['foo', 'bar', 'baz'],
['a', 1, True],
['b', 2, True],
['b', 3]]
actual = mergeduplicates(table, 'foo')
expect = [('foo', 'bar', 'baz'),
('a', 1, True),
('b', Conflict([2, 3]), True)]
ieq(expect, actual)
def test_mergeduplicates_compoundkey():
table = [['foo', 'bar', 'baz'],
['a', 1, True],
['a', 1, True],
['a', 2, False],
['a', 2, None],
['c', 3, True],
['c', 3, False],
]
actual = mergeduplicates(table, key=('foo', 'bar'))
expect = [('foo', 'bar', 'baz'),
('a', 1, True),
('a', 2, False),
('c', 3, Conflict([True, False]))]
ieq(expect, actual)
def test_fold():
t1 = (('id', 'count'), (1, 3), (1, 5), (2, 4), (2, 8))
t2 = fold(t1, 'id', operator.add, 'count', presorted=True)
expect = (('key', 'value'), (1, 8), (2, 12))
ieq(expect, t2)
ieq(expect, t2)