From 4865d2cb2ec611a79dd954147a991d1b4cdfa050 Mon Sep 17 00:00:00 2001 From: Stefan Garlonta Date: Tue, 20 Jun 2023 14:03:41 +0200 Subject: [PATCH] Add `sum()` operation to numerical streams --- pystreamapi/_streams/numeric/__numeric_base_stream.py | 7 +++++++ pystreamapi/_streams/numeric/__parallel_numeric_stream.py | 6 ++++++ .../_streams/numeric/__sequential_numeric_stream.py | 5 +++++ tests/test_numeric_stream_implementation.py | 8 ++++++++ 4 files changed, 26 insertions(+) diff --git a/pystreamapi/_streams/numeric/__numeric_base_stream.py b/pystreamapi/_streams/numeric/__numeric_base_stream.py index 91f1de7..a42684f 100644 --- a/pystreamapi/_streams/numeric/__numeric_base_stream.py +++ b/pystreamapi/_streams/numeric/__numeric_base_stream.py @@ -75,6 +75,13 @@ def range(self) -> Union[float, int, None]: self._trigger_exec() return max(self._source) - min(self._source) if len(self._source) > 0 else None + @abstractmethod + def sum(self) -> Union[float, int, None]: + """ + Calculates the sum of all items of a numerical stream + :return: The sum, can be int or float + """ + def third_quartile(self) -> Union[float, int, None]: """ Calculates the third quartile of a numerical Stream diff --git a/pystreamapi/_streams/numeric/__parallel_numeric_stream.py b/pystreamapi/_streams/numeric/__parallel_numeric_stream.py index 2760a18..d8f2c2f 100644 --- a/pystreamapi/_streams/numeric/__parallel_numeric_stream.py +++ b/pystreamapi/_streams/numeric/__parallel_numeric_stream.py @@ -12,6 +12,12 @@ def mean(self) -> Union[float, int, None]: self._trigger_exec() return self.__sum() / len(self._source) if len(self._source) > 0 else None + def sum(self) -> Union[float, int, None]: + """Calculates the sum of values""" + self._trigger_exec() + _sum = self.__sum() + return 0 if _sum == [] else _sum + def __sum(self): """Parallel sum method""" self._set_parallelizer_src() diff --git a/pystreamapi/_streams/numeric/__sequential_numeric_stream.py b/pystreamapi/_streams/numeric/__sequential_numeric_stream.py index 658b75d..d6873a3 100644 --- a/pystreamapi/_streams/numeric/__sequential_numeric_stream.py +++ b/pystreamapi/_streams/numeric/__sequential_numeric_stream.py @@ -11,3 +11,8 @@ def mean(self) -> Union[float, int, None]: """Calculates mean of values""" self._trigger_exec() return sum(self._source) / len(self._source) if len(self._source) > 0 else None + + def sum(self) -> Union[float, int, None]: + """Calculates the sum of values""" + self._trigger_exec() + return sum(self._source) diff --git a/tests/test_numeric_stream_implementation.py b/tests/test_numeric_stream_implementation.py index f502386..232fda5 100644 --- a/tests/test_numeric_stream_implementation.py +++ b/tests/test_numeric_stream_implementation.py @@ -23,3 +23,11 @@ def test_mean_empty(self): def test_mean_negative(self): result = self.stream([-1, -2, -3, -4, -5]).mean() self.assertEqual(result, -3) + + def test_sum(self): + result = self.stream([1, 2, 3, 4, 5]).sum() + self.assertEqual(result, 15) + + def test_sum_empty(self): + result = self.stream([]).sum() + self.assertEqual(result, 0)