From aff3aa95557967e3fcc69de325bbcdbc0902d284 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Tue, 1 May 2018 13:49:11 -0400 Subject: [PATCH] add 'contributions distinct' command Add a tool to find sets of distinct values within a data set. Change-Id: I193ef507e3e3a688508c2902673cb9327f4e528c Signed-off-by: Doug Hellmann --- goal_tools/tests/test_distinct.py | 37 ++++++++++++++++++++ goal_tools/who_helped/distinct.py | 56 +++++++++++++++++++++++++++++++ setup.cfg | 1 + 3 files changed, 94 insertions(+) create mode 100644 goal_tools/tests/test_distinct.py create mode 100644 goal_tools/who_helped/distinct.py diff --git a/goal_tools/tests/test_distinct.py b/goal_tools/tests/test_distinct.py new file mode 100644 index 0000000..0579623 --- /dev/null +++ b/goal_tools/tests/test_distinct.py @@ -0,0 +1,37 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from goal_tools.who_helped import distinct +from goal_tools.tests import base + + +class TestGetDistinct(base.TestCase): + + _data = [ + {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}, + {'a': 'A', 'b': 'C', 'c': 'D', 'd': 'D'}, + ] + + def test_group_by_one_column(self): + results = distinct._get_distinct(['a'], self._data) + expected = set([ + ('A',), + ]) + self.assertEqual(expected, results) + + def test_group_by_two_columns(self): + results = distinct._get_distinct(['a', 'b'], self._data) + expected = set([ + ('A', 'B'), + ('A', 'C'), + ]) + self.assertEqual(expected, results) diff --git a/goal_tools/who_helped/distinct.py b/goal_tools/who_helped/distinct.py new file mode 100644 index 0000000..9dad930 --- /dev/null +++ b/goal_tools/who_helped/distinct.py @@ -0,0 +1,56 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import logging + +from goal_tools.who_helped import contributions +from goal_tools.who_helped import report + +LOG = logging.getLogger(__name__) + + +def _get_distinct(by_names, data_source): + return set( + tuple(row[b] for b in by_names) + for row in data_source + ) + + +class DistinctContributions(report.ContributionsReportBase): + "Show distinct values in a contribution report." + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument( + '--by', '-b', + action='append', + default=[], + choices=contributions._COLUMNS, + help=('column(s) to summarize by (may be repeated), ' + 'defaults to "Organization"'), + ) + return parser + + def take_action(self, parsed_args): + group_by = parsed_args.by[:] + if not group_by: + group_by.append('Organization') + + data = self.get_contributions(parsed_args) + + values = _get_distinct(group_by, data) + + output_rows = sorted(values) + + columns = tuple(group_by) + + return (columns, output_rows) diff --git a/setup.cfg b/setup.cfg index 63c5060..3603c91 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,6 +31,7 @@ console_scripts = who_helped = contributions list = goal_tools.who_helped.contributions:ListContributions contributions summarize = goal_tools.who_helped.summarize:SummarizeContributions + contributions distinct = goal_tools.who_helped.distinct:DistinctContributions member show = goal_tools.who_helped.members:ShowMember changes query = goal_tools.who_helped.changes:QueryChanges cache remove = goal_tools.who_helped.cache:CacheRemove