Skip to content

Commit

Permalink
add __call__ as alias for .count
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Oct 16, 2024
1 parent 8947721 commit 81ac8fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions streamable/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def count(self) -> int:

return sum(1 for _ in self)

def __call__(self) -> int:
"""
Iterates over this stream until exhaustion and returns the count of elements.
Equivalent to `stream.count()`.
Returns:
int: Number of elements yielded during an entire iteration over this stream.
"""
return self.count()

def display(self, level: int = logging.INFO) -> "Stream[T]":
"""
Logs (INFO level) a representation of the stream.
Expand Down
9 changes: 7 additions & 2 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,10 +1356,15 @@ def effect(x: int) -> None:
l.append(x)

stream = Stream(lambda: map(effect, src))
self.assertIs(
self.assertEqual(
stream.count(),
N,
msg="`exhaust` should return self.",
msg="`count` should return the count of elements.",
)
self.assertEqual(
stream(),
N,
msg="`__call__` should return the count of elements.",
)
self.assertListEqual(
l, list(src), msg="`exhaust` should iterate over the entire stream."
Expand Down

0 comments on commit 81ac8fa

Please sign in to comment.