From db9da3f973a87bc8953e43372ed603373530cb68 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 20 Feb 2024 11:09:19 -0700 Subject: [PATCH 1/4] test: additional group_by tests --- tests/test_seqs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_seqs.py b/tests/test_seqs.py index 4a4c0e6..a1635b2 100644 --- a/tests/test_seqs.py +++ b/tests/test_seqs.py @@ -1,5 +1,5 @@ from collections.abc import Iterator -from operator import add +from operator import add, attrgetter, itemgetter import pytest from whatever import _ @@ -140,9 +140,13 @@ def test_split_by(): def test_group_by(): assert group_by(_ % 2, range(5)) == {0: [0, 2, 4], 1: [1, 3]} assert group_by(r'\d', ['a1', 'b2', 'c1']) == {'1': ['a1', 'c1'], '2': ['b2']} + assert group_by( + itemgetter("x"), [{"x": 1, "y": 1}, {"x": 2, "y": 2}, {"x": 1, "y": 3}] + ) == {1: [{"x": 1, "y": 1}, {"x": 1, "y": 3}], 2: [{"x": 2, "y": 2}]} def test_group_by_keys(): assert group_by_keys(r'(\d)(\d)', ['12', '23']) == {'1': ['12'], '2': ['12', '23'], '3': ['23']} + assert group_by_keys(str.split, ["hello man", "hello woman"]) == {'hello': ['hello man', 'hello woman'], 'man': ['hello man'], 'woman': ['hello woman']} def test_group_values(): assert group_values(['ab', 'ac', 'ba']) == {'a': ['b', 'c'], 'b': ['a']} From e06b00843b7e186431f17a9d908552daf875a4e5 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 20 Feb 2024 11:09:29 -0700 Subject: [PATCH 2/4] docs: additional group_by docs --- docs/seqs.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/seqs.rst b/docs/seqs.rst index 379f279..73c5a60 100644 --- a/docs/seqs.rst +++ b/docs/seqs.rst @@ -416,13 +416,23 @@ Split and chunk One can use :func:`split` when grouping by boolean predicate. See also :func:`py3:itertools.groupby`. + You can also easily group dictionaries: + + :: + + group_by(itemgetter("age"), [{"age": 10, "name": "Alice"}, {"age": 12, "name": "Bob"}]) + # -> {10: [{'age': 10, 'name': 'Alice'}], 12: [{'age': 12, 'name': 'Bob'}]} + .. function:: group_by_keys(get_keys, seq) Groups elements of ``seq`` having multiple keys each into :class:`defaultdict(list) `. Can be used to reverse grouping:: posts_by_tag = group_by_keys(attrgetter('tags'), posts) + + sentences = ["hello man", "hello woman"] sentences_with_word = group_by_keys(str.split, sentences) + # -> {'hello': ['hello man', 'hello woman'], 'man': ['hello man'], 'woman': ['hello woman']} .. function:: group_values(seq) From 2d096cd2556c8814b3ee42f96a1262e09928af09 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 20 Feb 2024 11:12:57 -0700 Subject: [PATCH 3/4] style: fix unused import --- tests/test_seqs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_seqs.py b/tests/test_seqs.py index a1635b2..b41ef1b 100644 --- a/tests/test_seqs.py +++ b/tests/test_seqs.py @@ -1,5 +1,5 @@ from collections.abc import Iterator -from operator import add, attrgetter, itemgetter +from operator import add, itemgetter import pytest from whatever import _ From 39410dc7d6fc125563d14904ef777ebaa9cb23c8 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 20 Feb 2024 11:13:28 -0700 Subject: [PATCH 4/4] style: fix line length --- tests/test_seqs.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_seqs.py b/tests/test_seqs.py index b41ef1b..6f072a3 100644 --- a/tests/test_seqs.py +++ b/tests/test_seqs.py @@ -146,7 +146,9 @@ def test_group_by(): def test_group_by_keys(): assert group_by_keys(r'(\d)(\d)', ['12', '23']) == {'1': ['12'], '2': ['12', '23'], '3': ['23']} - assert group_by_keys(str.split, ["hello man", "hello woman"]) == {'hello': ['hello man', 'hello woman'], 'man': ['hello man'], 'woman': ['hello woman']} + assert group_by_keys(str.split, ["hello man", "hello woman"]) == { + 'hello': ['hello man', 'hello woman'], 'man': ['hello man'], 'woman': ['hello woman'] + } def test_group_values(): assert group_values(['ab', 'ac', 'ba']) == {'a': ['b', 'c'], 'b': ['a']}