Skip to content

sliding_window() recipe: Raise ValueError for non-positive window sizes. Add more tests. #105403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2023

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Jun 6, 2023

The existing recipe was giving bogus results for n=0:

>>> list(sliding_window('ABCDEFG', 0))
[(), ('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]

  • Fix and simplify code
  • Add better error checking
  • Add more extensive tests

📚 Documentation preview 📚: https://cpython-previews--105403.org.readthedocs.build/

@rhettinger rhettinger added docs Documentation in the Doc dir skip issue skip news needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Jun 6, 2023
@rhettinger rhettinger merged commit 423459b into python:main Jun 6, 2023
@miss-islington
Copy link
Contributor

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @rhettinger, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 423459be2f0b6d007e5f235f39d80044cb099faf 3.11

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 6, 2023
…es. Add more tests. (pythonGH-105403)

(cherry picked from commit 423459b)

Co-authored-by: Raymond Hettinger <[email protected]>
@bedevere-bot
Copy link

GH-105405 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jun 6, 2023
rhettinger pushed a commit that referenced this pull request Jun 6, 2023
@rhettinger rhettinger deleted the fix_sliding_window_recipe branch June 6, 2023 19:00
@pochmann
Copy link
Contributor

pochmann commented Jun 26, 2023

The existing recipe was giving bogus results for n=0:

>>> list(sliding_window('ABCDEFG', 0))
[(), ('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]

That would mean that deque violates maxlen=0. Are you sure? That would be a bug in it's own right.

I was getting [(), (), (), (), (), (), (), ()] as expected. Btw I once was about to suggest what you did here, but realized it would stop supporting the n=0 case. I thought the old longer code was like that in order to support n=0. Was that not the case? I find unnecessarily breaking n=0 disappointing. It used to correspond to for example 'ABCDEFG'.count('') simply returning 8, just like the recipe used to yield 8 empty windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants