Skip to content

bpo-30296 Remove unnecessary tuples, lists, sets, and dicts#1489

Merged
rhettinger merged 1 commit into
python:masterfrom
jdufresne:comprehensions
May 18, 2017
Merged

bpo-30296 Remove unnecessary tuples, lists, sets, and dicts#1489
rhettinger merged 1 commit into
python:masterfrom
jdufresne:comprehensions

Conversation

@jdufresne

Copy link
Copy Markdown
Contributor
  • Replaced list() with list comprehension
  • Replaced dict() with dict comprehension
  • Replaced set() with set comprehension
  • Replaced builtin func() with func() when supported (e.g. any(), all(), tuple(), min(), &
    max())

@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@jdufresne jdufresne changed the title Remove unnecessary tuples, lists, sets, and dicts bpo-30296 Remove unnecessary tuples, lists, sets, and dicts May 7, 2017
Comment thread Lib/_weakrefset.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's use: set(map(ref, other)) for all of these.

Comment thread Lib/distutils/msvc9compiler.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes to this one. Set literals are always a win.

Comment thread Lib/inspect.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, this one is fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Comment thread Lib/ntpath.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code smells bad (both the original and revision). Can this be replaced with any(...).

Comment thread Lib/optparse.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like a reasonable change, but optparse is essentially defunct so we should leave it alone.

Comment thread Tools/stringbench/stringbench.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I prefer not to touch a benchmark file lest it cause comparability issues.

Comment thread setup.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Skip this. I prefer not to touch anything in setup.py.

Comment thread setup.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Skip

Comment thread setup.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Skip

Comment thread setup.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ambivalent.

@terryjreedy terryjreedy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I keep idlelib 3.6 and 3.7 in sync as much as possible.
Please remove all Lib/idlelib/multicall.py changes from this PR and optionally (and preferably) make the 2 approved changes in a separate PR that can be backported easily.

Comment thread Lib/idlelib/multicall.py Outdated

@terryjreedy terryjreedy May 12, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I prefer the change as easier to read and likely faster. Ditto for the other dict(list) to dict comp change in this file. (Agreeing with Raymond.)

Comment thread Lib/idlelib/multicall.py Outdated

@terryjreedy terryjreedy May 12, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agree with Raymond, leave as is.

@jdufresne

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! I believe I have addressed all feedback comments. If I missed or misunderstood any of the previous comments, please let me know.

* Replaced list(<generator expression>) with list comprehension
* Replaced dict(<generator expression>) with dict comprehension
* Replaced set(<list literal>) with set literal
* Replaced builtin func(<list comprehension>) with func(<generator
  expression>) when supported (e.g. any(), all(), tuple(), min(), &
  max())
@rhettinger rhettinger merged commit 3972628 into python:master May 18, 2017
Comment thread Lib/email/headerregistry.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The original variant, with list comprehension, is faster than the variant with a generator. The difference is up to 2 times for simple tests.

I would keep the original code.

Comment thread Lib/inspect.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Comment thread Lib/logging/config.py
props = config.pop('.', None)
# Check for valid identifiers
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
kwargs = dict((k, config[k]) for k in config if valid_ident(k))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would use a dict comprehension.

And the same for other change in this file.

Comment thread Lib/pstats.py
# format used by cProfile
new_callers[func] = tuple([i[0] + i[1] for i in
zip(caller, new_callers[func])])
new_callers[func] = tuple(i[0] + i[1] for i in zip(caller, new_callers[func]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Maybe unpack a tuple:

tuple([i + j for i, j in zip(caller, new_callers[func])])

Comment thread Lib/symtable.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Comment thread Lib/turtle.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Comment thread Lib/turtle.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

.keys() can be removed. Iterate just docsdict.

Comment thread Lib/urllib/request.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Comment thread Lib/urllib/request.py
headers = dict(req.unredirected_hdrs)
headers.update(dict((k, v) for k, v in req.headers.items()
if k not in headers))
headers.update((k, v) for k, v in req.headers.items() if k not in headers)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This changes the semantic. Updated headers is used in the generator expression. I don't know whether this is good.

I would use a dict comprehesion.

Comment thread Tools/gdb/libpython.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would keep the original code.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jul 7, 2017
@jdufresne jdufresne deleted the comprehensions branch November 18, 2017 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants