Commit MetaInfo

Revisiond3beb8e84c190c79096a558af465966036573b64 (tree)
Time2022-06-23 22:24:31
AuthorAntoon Pardon <aej@pard...>
CommiterAntoon Pardon

Log Message

Select iterator added

Change Summary

Incremental Difference

diff -r 85d3528d67b4 -r d3beb8e84c19 prepare
--- a/prepare Wed Jun 22 22:29:01 2022 +0200
+++ b/prepare Thu Jun 23 15:24:31 2022 +0200
@@ -1,18 +1,6 @@
11 #!/bin/bash
22
33 set -e
4-set -x
5-
6-echo "${0%/*}"
7-pushd "${0%/*}"
8-PROJECTDIRECTORY=$(pwd)
9-popd
10-
11-PATH=$PATH:$PROJECTDIRECTORY/bin
12-
13-make all
14-
15-cd $PROJECTDIRECTORY
164
175 ENVIRONMENT=conda3.10
186 PYTHONPATH=$(type -p python3)
@@ -27,6 +15,17 @@
2715 exit 1
2816 esac
2917
18+echo "${0%/*}"
19+pushd "${0%/*}" > /dev/null
20+PROJECTDIRECTORY=$(pwd)
21+popd > /dev/null
3022
31-pushd tests; make all; popd
32-pushd source; make all; popd
23+PATH=$PATH:$PROJECTDIRECTORY/bin
24+
25+make all
26+
27+cd $PROJECTDIRECTORY
28+
29+
30+pushd tests > /dev/null; make all; popd > /dev/null
31+pushd source > /dev/null; make all; popd > /dev/null
diff -r 85d3528d67b4 -r d3beb8e84c19 source/iterators.py
--- a/source/iterators.py Wed Jun 22 22:29:01 2022 +0200
+++ b/source/iterators.py Thu Jun 23 15:24:31 2022 +0200
@@ -98,5 +98,14 @@
9898 # [[1, 1], [2, 3, 5], [8]] | concat -> 1 1 2 3 5 8
9999 # """
100100
101-concat = cast(Callable[ [ Iterable[Any] ], Iterable[Any] ], Apply(chain.from_iterable))
101+#concat = cast(Callable[ [ Iterable[Any] ], Iterable[Any] ], Apply(chain.from_iterable))
102102 concat = Apply(chain.from_iterable) # type: ignore
103+
104+class Select(PipeIterator):
105+ """
106+ This class produces a pipe iterator that only lets items through that are
107+ selected through the condition.
108+ """
109+ # pylint: disable=too-few-public-methods
110+ def __init__(self, condition: Callable[[Iterator[Any]], Any] ):
111+ super().__init__(partial(filter, condition))
diff -r 85d3528d67b4 -r d3beb8e84c19 source/release.cfg
--- a/source/release.cfg Wed Jun 22 22:29:01 2022 +0200
+++ b/source/release.cfg Thu Jun 23 15:24:31 2022 +0200
@@ -1,3 +1,3 @@
1-version = '0.01.00j'
2-modified = '2022-06-22 @ 22:27:11'
1+version = '0.01.00k'
2+modified = '2022-06-23 @ 15:23:41'
33 installed = '*********************'
diff -r 85d3528d67b4 -r d3beb8e84c19 tests/iterators.py
--- a/tests/iterators.py Wed Jun 22 22:29:01 2022 +0200
+++ b/tests/iterators.py Thu Jun 23 15:24:31 2022 +0200
@@ -5,7 +5,7 @@
55 from typing import List
66
77 # pylint: disable=unused-import
8-from AGPlib.iterators import PipeIterator, Apply, Map, concat, SubMap
8+from AGPlib.iterators import PipeIterator, Apply, Map, concat, SubMap, Select
99 # pylint: enable=unused-import
1010
1111 def shuffled_range_list(*args) -> List[int]:
@@ -30,6 +30,9 @@
3030 def is_odd(nr: int) -> bool:
3131 return bool(nr % 2)
3232
33+def is_larger_than_50(nr: int) -> bool:
34+ return nr > 50
35+
3336 def collatz(nr: int) -> int:
3437 return 3 * nr + 1 if is_odd(nr) else nr // 2
3538
@@ -82,4 +85,15 @@
8285 s.assertEqual(result0, result1)
8386 s.assertEqual(result0, result2)
8487
88+ def test_select(s):
89+ # pylint: disable=redefined-outer-name
90+ List = Apply(list)
91+ source = shuffled_range_list(100)
92+ for condition in [is_odd, is_larger_than_50]:
93+ result0 = [nr for nr in source if condition(nr)]
94+ result1 = source | Select(condition) | List
95+ result2 = source >> Select(condition) >> List
96+ s.assertEqual(result0, result1)
97+ s.assertEqual(result0, result2)
98+
8599 unitmain()
Show on old repository browser