Module osbot_utils.decorators.lists.group_by
Expand source code
#todo: refactor with index_by
from functools import wraps
def group_by(function): # returns the list provided grouped by the key provided in group_by
@wraps(function)
def wrapper(*args,**kwargs):
key = None
if 'group_by' in kwargs:
key = kwargs.get('group_by')
del kwargs['group_by']
values = function(*args,**kwargs)
if key:
results = {}
for item in values:
value = item.get(key)
if results.get(value) is None: results[value] = []
results[value].append(item)
return results
return values
return wrapper
Functions
def group_by(function)
-
Expand source code
def group_by(function): # returns the list provided grouped by the key provided in group_by @wraps(function) def wrapper(*args,**kwargs): key = None if 'group_by' in kwargs: key = kwargs.get('group_by') del kwargs['group_by'] values = function(*args,**kwargs) if key: results = {} for item in values: value = item.get(key) if results.get(value) is None: results[value] = [] results[value].append(item) return results return values return wrapper