63 lines
1.2 KiB
Python
63 lines
1.2 KiB
Python
from collections import defaultdict
|
|
from itertools import product
|
|
# from fn.func import curried
|
|
|
|
def pipe(x):
|
|
return x
|
|
|
|
|
|
def print_pipe(x):
|
|
print(x)
|
|
return x
|
|
|
|
|
|
def flattenDict(l):
|
|
def tupalize(k, vs):
|
|
l = []
|
|
if isinstance(vs, list):
|
|
for v in vs:
|
|
l.append((k, v))
|
|
else:
|
|
l.append((k, vs))
|
|
return l
|
|
|
|
flat_list = [tupalize(k, vs) for k, vs in l.items()]
|
|
flat_dict = [dict(items) for items in product(*flat_list)]
|
|
return flat_dict
|
|
|
|
|
|
def flatten(l):
|
|
if isinstance(l, list):
|
|
return [item for sublist in l for item in sublist]
|
|
elif isinstance(l, dict):
|
|
return flattenDict(l)
|
|
|
|
|
|
def drop_right(l, n):
|
|
return l[:len(l)-n]
|
|
|
|
# def flatmap(f, items):
|
|
# return list(map(f, items))
|
|
|
|
|
|
def key_filter(l, keyname):
|
|
return [v[keyname] for k, v in l.items()]
|
|
|
|
|
|
def groupByKey(l):
|
|
d = defaultdict(list)
|
|
for key, value in l:
|
|
d[key].append(value)
|
|
return list(dict(d).items()).pop()
|
|
|
|
|
|
# @curried
|
|
def rename(new_name, f):
|
|
f.__name__ = new_name
|
|
return f
|
|
#
|
|
# def rename(newname):
|
|
# def decorator(f):
|
|
# f.__name__ = newname
|
|
# return f
|
|
# return decorator |