-
Notifications
You must be signed in to change notification settings - Fork 11
/
table_operations.py
54 lines (47 loc) · 1007 Bytes
/
table_operations.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
from typing import (
Any,
List,
Tuple,
)
def min_aggregation(
data: List[Tuple[Any]],
key_fxn,
value_fxn,
):
'''
This is the equivalent of the sql statement:
select
a,
b,
min(x)
from data
group by a,b
(a, b) as :key_fxn
x is :value_fxn
'''
min_by_key = {}
for row in data:
key = key_fxn(row)
value = value_fxn(row)
if key in min_by_key:
if value < min_by_key[key]:
min_by_key[key] = value
else:
min_by_key[key] = value
out_rows = []
for key, val in min_by_key.items():
out_rows.append(key + (val,))
return out_rows
def group_by(
data: List[Tuple[Any]],
key_fxn,
values_fxn,
):
by_key = {}
for row in data:
key = key_fxn(row)
value = values_fxn(row)
if key not in by_key:
by_key[key] = []
by_key[key].append(value)
return by_key