◐ Shell
reader mode source ↗
Skip to content

bpo-4356: Add key function support to the bisect module#20556

Merged
rhettinger merged 7 commits into
python:masterfrom
rhettinger:bisect_key
Oct 20, 2020
Merged

bpo-4356: Add key function support to the bisect module#20556
rhettinger merged 7 commits into
python:masterfrom
rhettinger:bisect_key

Conversation

@rhettinger

@rhettinger rhettinger commented May 31, 2020

Copy link
Copy Markdown
Contributor

@remilapeyre

Copy link
Copy Markdown

Hi @rhettinger, was there anything wrong with #11781 ?

@rhettinger

rhettinger commented May 31, 2020

Copy link
Copy Markdown
Contributor Author

Hmm, I didn't see that PR until after this one was made.

Looking at it now, I prefer this one which has cleaner code, better docs updates, and focuses on key and leaves out reversed which was not requested by the OP (if that turns out to be needed, it can be added later; for now, it is better to avoid doubling the code complexity).

@remilapeyre

Copy link
Copy Markdown

I don't care which version gets merged but regarding the reversed argument you said in the review:

FWIW, if key-function support is added, it is inevitable that reversed support will be requested (since sorted() and bisect() are often used together).

@rhettinger

Copy link
Copy Markdown
Contributor Author

Also, some of the tests should be combined. Both PRs have tests that the others don't.

Am unsure about reversed. It seems like a nice to have, but looking at the other PR, it doubles the code complexity and that spills into the docs/tests as well. What do you think?

@remilapeyre

remilapeyre commented May 31, 2020

Copy link
Copy Markdown

For reversed, most users that know they need bisect should be able to write the key function so that it gives the correct order directly and don't need the help of reversed. It's not part of the original request and complicates both the Python and C code.

If it's ever needed you could add it later but since it increases the complexity without adding functionality, it may be best not to add it.

There is a note recommending lru_cache() (maybe just cache() now) which I probably would not have thought of and may be worth keeping:

When specifying a custom key function, you should wrap it with
:func:functools.lru_cache if the key function is not already fast.

Edit: Nevermind, I read the doc better and it's actually here.

@rhettinger

Copy link
Copy Markdown
Contributor Author

BTW, do you know how to appease the buildbots for the doc build. I'm unclear on what they deem "suspicious" and how to clear the blocker.

@remilapeyre

remilapeyre commented May 31, 2020

Copy link
Copy Markdown

You can fix the spurious error os "suspicious" by removing lines 114 and 115 of Doc/tools/susp-ignored.csv:

library/bisect,32,:hi,all(val >= x for val in a[i:hi])
library/bisect,42,:hi,all(val > x for val in a[i:hi])

@rhettinger

Copy link
Copy Markdown
Contributor Author

Thanks.

Apologies again for duplicating work. I completely forget about the other PR.

@remilapeyre remilapeyre left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hide comment

Apologies again for duplicating work. I completely forget about the other PR.

Don't worry, at least it's great the issue can be closed!

@rhettinger rhettinger merged commit 871934d into python:master Oct 20, 2020
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
@wpk-

wpk- commented May 3, 2022

Copy link
Copy Markdown

Somehow bisect(lst, fn(x), key=fn) feels very uncomfortable. After all, we're looking to find the insertion point for x in lst, rather than fn(x). I'd really expect the syntax to be bisect(lst, x, key=fn). Would the Python devs be open for such a change?

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.

9 participants