Skip to content
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

gh-126317: Simplify stdlib code by using itertools.batched() #126323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dongwooklee96
Copy link

@dongwooklee96 dongwooklee96 commented Nov 2, 2024

Copy link

cpython-cla-bot bot commented Nov 2, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Nov 2, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@corona10
Copy link
Member

corona10 commented Nov 2, 2024

@serhiy-storchaka

@dongwooklee96 is the first-time contributor to the PyCon KR sprint today.
I am not sure if this change is worth accepting from my side.
So I am delegating the review to you :)

@@ -1049,14 +1048,11 @@ def _batch_appends(self, items, obj):
write(APPENDS)
elif n:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be replaced with else, since batched() does not emit empty batches.

@@ -1035,12 +1035,11 @@ def _batch_appends(self, items, obj):

it = iter(items)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is no longer needed. items can be used directly.

@@ -1087,12 +1083,11 @@ def _batch_setitems(self, items, obj):
return

it = iter(items)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@@ -1101,17 +1096,14 @@ def _batch_setitems(self, items, obj):
raise
write(SETITEMS)
elif n:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@@ -1125,8 +1117,7 @@ def save_set(self, obj):
self.memoize(obj)

it = iter(obj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Comment on lines 1121 to 1122
n = len(batch)
if n > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer needed.

n = len(tmp)
for batch in batched(it, self._BATCHSIZE):
n = len(batch)
if n > 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can inline n which is only used one time. Also, it would be safer to write the condition as len(batch) != 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants