From 708b1782c948a302ac887e2af82bfc13ae74ff67 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Thu, 23 May 2024 16:00:48 +0200 Subject: [PATCH] make ResultContainer iterable --- bfabric/results/result_container.py | 5 ++++- bfabric/tests/unit/test_result_container.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bfabric/results/result_container.py b/bfabric/results/result_container.py index b8bd3a48..7f714b8c 100644 --- a/bfabric/results/result_container.py +++ b/bfabric/results/result_container.py @@ -1,7 +1,7 @@ from __future__ import annotations import logging -from typing import Any, TYPE_CHECKING +from typing import Any, TYPE_CHECKING, Iterable import bfabric.results.response_format_dict as formatter @@ -30,6 +30,9 @@ def __init__( def __getitem__(self, idx: int) -> dict[str, Any]: return self.results[idx] + def __iter__(self) -> Iterable[dict[str, Any]]: + return iter(self.results) + def __repr__(self) -> str: return self.__str__() diff --git a/bfabric/tests/unit/test_result_container.py b/bfabric/tests/unit/test_result_container.py index 6591fa89..6c24aa1e 100644 --- a/bfabric/tests/unit/test_result_container.py +++ b/bfabric/tests/unit/test_result_container.py @@ -20,6 +20,10 @@ def test_repr(self): self.assertEqual("[1, 2, 3]", repr(self.res1)) self.assertEqual("[4, 5]", repr(self.res2)) + def test_iter(self): + items = list(iter(self.res1)) + self.assertListEqual([1, 2, 3], items) + def test_len(self): self.assertEqual(3, len(self.res1)) self.assertEqual(2, len(self.res2))