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 <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-05-01 13:49:11 -04:00
parent 5af6614932
commit aff3aa9555
3 changed files with 94 additions and 0 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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