summaryrefslogtreecommitdiffstats
path: root/hostinfo/utils.py
blob: 379b9fa2834ba312329cc32fd986a64fb4336ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: utf-8 -*-

def group_by(value, group, default=None, display_check=None, print_value=None):
    groups = {}
    if value is None:
        return groups

    for elem in value:
        key = None
        if group in elem:
            key = elem[group]
        else:
            key = default

        if display_check is None or display_check(key):
            output = elem
            if print_value is not None:
                output = print_value(elem)

            groups.setdefault(key, []).append(output)

    return groups