-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.py
31 lines (30 loc) · 1.08 KB
/
shard.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
# shard_by is a list of (key function, pretty key, comparable key)
def shard(data, shard_by, indent=0, key=str):
result = {}
key_func, pretty_func, comparable, *nop = shard_by[0] + (int,)
shard_by = shard_by[1:]
sep = '\n' + '\t' * indent
for i in data:
k = key_func(i)
result[k] = result.get(k, {})
result[k][i] = data[i]
if shard_by:
for i in result:
result[i] = shard(result[i], shard_by, indent+1, key)
else:
for i in result:
try:
sorted_data=sorted(result[i].items(), key=lambda x:key(x[0]))
except Exception:
sorted_data = sorted(result[i].items())
result[i] = '\n' + '\n'.join(i[1] for i in sorted_data)
result[i] = result[i].replace('\n', (sep + '\t'))
try:
keys = sorted(result, key=comparable)
except Exception:
keys = sorted(result)
ret = []
for i in keys:
ret.append(sep + pretty_func(i) + ':')
ret.append(result[i])
return (''.join(ret) if indent else ''.join(ret).lstrip())