-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
bpo-31417 Use the enumerate function where appropriate. #3497
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -795,14 +795,14 @@ def findsource(object): | |
| # use the one with the least indentation, which is the one | ||
| # that's most probably not inside a function definition. | ||
| candidates = [] | ||
| for i in range(len(lines)): | ||
| match = pat.match(lines[i]) | ||
| for linenum, line in enumerate(lines): | ||
| match = pat.match(line) | ||
| if match: | ||
| # if it's at toplevel, it's already the best one | ||
| if lines[i][0] == 'c': | ||
| return lines, i | ||
| if line[0] == 'c': | ||
| return lines, linenum | ||
| # else add whitespace to candidate list | ||
| candidates.append((match.group(1), i)) | ||
| candidates.append((match.group(1), linenum)) | ||
| if candidates: | ||
| # this will sort by whitespace, and by line number, | ||
| # less whitespace first | ||
|
|
@@ -1258,8 +1258,8 @@ def convert(name, locals=locals, | |
| formatarg=formatarg, formatvalue=formatvalue): | ||
| return formatarg(name) + formatvalue(locals[name]) | ||
| specs = [] | ||
| for i in range(len(args)): | ||
| specs.append(convert(args[i])) | ||
| for i, arg in enumerate(args): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to keep the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern can be seen in places in the repository. The original code also did not use a standard: I focused on replacing range(len(args)) with enumerate(args). |
||
| specs.append(convert(arg)) | ||
| if varargs: | ||
| specs.append(formatvarargs(varargs) + formatvalue(locals[varargs])) | ||
| if varkw: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -554,9 +554,9 @@ def replace_paths_in_code(self, co): | |
| self.processed_paths.append(original_filename) | ||
|
|
||
| consts = list(co.co_consts) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we modify this function, I suggest to move type(co) out of the loop: code_type = type(co) |
||
| for i in range(len(consts)): | ||
| if isinstance(consts[i], type(co)): | ||
| consts[i] = self.replace_paths_in_code(consts[i]) | ||
| for i, const in enumerate(consts): | ||
| if isinstance(const, type(co)): | ||
| consts[i] = self.replace_paths_in_code(const) | ||
|
|
||
| return types.CodeType(co.co_argcount, co.co_kwonlyargcount, | ||
| co.co_nlocals, co.co_stacksize, co.co_flags, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 stell leaves the
obj = objects[i], why not justfor src, obj in zip(sources, objects):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.
I tried to stay close to the original code and do what was needed to replace
range(len(sources))withenumerate(sources).Your suggestion could be done but it looks like the comments in the bug tracker would prefer no changes at all.