Skip to content

Commit

Permalink
Merge 1bd022c into b2586a0
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d3rman committed Oct 8, 2023
2 parents b2586a0 + 1bd022c commit 6b77d7b
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -84,4 +84,5 @@ Source Contributors
- Josh Kim `@jsk56143 <https://github.com/jsk56143>`_
- Rolf Campbell `@endlisnis <https://github.com/endlisnis>`_
- zacc `@zacc <https://github.com/zacc>`_
- c0d3rman `@c0d3rman <https://github.com/c0d3rman>`
- Add "Name <email (optional)> and github profile link" above this line.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -16,6 +16,8 @@ Unreleased
templates.
- :meth:`~.SubredditRedditorFlairTemplates.reorder` to reorder a subreddit's redditor
flair templates.
- :func:`.stream_generator` now accepts the ``continue_after_id`` parameter, which
starts the stream after a given item ID.

**Changed**

Expand Down
13 changes: 11 additions & 2 deletions praw/models/util.py
Expand Up @@ -35,7 +35,12 @@ def permissions_string(


@_deprecate_args(
"function", "pause_after", "skip_existing", "attribute_name", "exclude_before"
"function",
"pause_after",
"skip_existing",
"attribute_name",
"exclude_before",
"continue_after_id",
)
def stream_generator(
function: Callable,
Expand All @@ -44,6 +49,7 @@ def stream_generator(
exclude_before: bool = False,
pause_after: int | None = None,
skip_existing: bool = False,
continue_after_id: str | None = None,
**function_kwargs: Any,
) -> Generator[Any, None, None]:
"""Yield new items from ``function`` as they become available.
Expand All @@ -62,6 +68,9 @@ def stream_generator(
:param skip_existing: When ``True``, this does not yield any results from the first
request thereby skipping any items that existed in the stream prior to starting
the stream (default: ``False``).
:param continue_after_id: The initial item ID value to use for ``before`` in
``params``. The stream will continue from the item following this one (default:
``None``).
Additional keyword arguments will be passed to ``function``.
Expand Down Expand Up @@ -122,7 +131,7 @@ def stream_generator(
print(comment)
"""
before_attribute = None
before_attribute = continue_after_id
exponential_counter = ExponentialCounter(max_counter=16)
seen_attributes = BoundedSet(301)
without_before_counter = 0
Expand Down

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tests/integration/models/reddit/test_subreddit.py
Expand Up @@ -926,6 +926,16 @@ def test_comments(self, reddit):
for i in range(400):
assert isinstance(next(generator), Comment)

def test_comments__with_continue_after_id(self, reddit):
subreddit = reddit.subreddit(pytest.placeholders.test_subreddit)
initial_stream = subreddit.stream.comments()
first_ten = [next(initial_stream) for _ in range(10)]
generator = subreddit.stream.comments(continue_after_id=first_ten[4].fullname)
for i in range(5):
comment = next(generator)
assert isinstance(comment, Comment)
assert comment.fullname == first_ten[i + 5].fullname

def test_comments__with_pause(self, reddit):
comment_stream = reddit.subreddit("kakapo").stream.comments(pause_after=0)
comment_count = 1
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/models/test_util.py
Expand Up @@ -124,3 +124,33 @@ def generate(limit, **kwargs):
thing = next(stream)
assert thing not in seen
seen.add(thing)

def test_comments__with_continue_after_id(
self,
):
Thing = namedtuple("Thing", ["fullname"])
initial_things = [Thing(n) for n in reversed(range(100))]
counter = 99

def generate(limit, params=None, **kwargs):
nonlocal counter
counter += 1
sliced_things = initial_things
if params:
sliced_things = initial_things[
: next(
i
for i, thing in enumerate(initial_things)
if thing.fullname == params["before"]
)
]
if counter % 2 == 0:
return sliced_things
return [Thing(counter)] + sliced_things[:-1]

stream = stream_generator(generate, continue_after_id=49)
expected_fullname = 50
for _ in range(50):
thing = next(stream)
assert thing.fullname == expected_fullname, thing
expected_fullname += 1

0 comments on commit 6b77d7b

Please sign in to comment.