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

def group_by(value, group, default=None, display_check=None, print_value=None):
    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