-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
base: main
Are you sure you want to change the base?
Conversation
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 |
@dongwooklee96 is the first-time contributor to the PyCon KR sprint today. |
@@ -1049,14 +1048,11 @@ def _batch_appends(self, items, obj): | |||
write(APPENDS) | |||
elif n: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
n = len(batch) | ||
if n > 0: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
.
itertools.batched()
#126317