diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2013-06-05 03:11:25 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2013-06-05 19:26:32 +0200 |
commit | 53aa9466e6c10f896425a178548867b4dd9781e2 (patch) | |
tree | 2bfc68d0ac428bb6bb632ca4f8abb6b286032deb /hostinfo/utils.py | |
parent | 67177a327c7e3cb542561c6b3d5352faab190d7b (diff) | |
download | tools-53aa9466e6c10f896425a178548867b4dd9781e2.tar.gz tools-53aa9466e6c10f896425a178548867b4dd9781e2.tar.bz2 tools-53aa9466e6c10f896425a178548867b4dd9781e2.zip |
utils: new group_by interface for generic grouping
The old function _group_by has now an own module (hostinfo.utils) and
got a slightly different interface. It is now used in all places,
where a grouping of the different values is required (printing,
searching, printing keys).
Diffstat (limited to 'hostinfo/utils.py')
-rw-r--r-- | hostinfo/utils.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/hostinfo/utils.py b/hostinfo/utils.py new file mode 100644 index 0000000..cfd8de3 --- /dev/null +++ b/hostinfo/utils.py @@ -0,0 +1,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 + |