-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
bpo-4356: Add key function support to the bisect module #20556
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d1a404
Python version, C version, and tests. Docs still needed
rhettinger 9c9b275
Add documentation
rhettinger 5f2e4e5
Add a blurb
rhettinger 92c40cb
Add spacing around the colon in the in-line code fragments
rhettinger bdfc074
Silence spurious warning from the doc build
rhettinger 5e3e5b9
Fix typo
rhettinger 899767c
Fix typo
rhettinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,8 +111,6 @@ howto/urllib2,,:password,"""joe:[email protected]""" | |
library/ast,,:upper,lower:upper | ||
library/ast,,:step,lower:upper:step | ||
library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)]," | ||
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]) | ||
library/configparser,,:home,my_dir: ${Common:home_dir}/twosheds | ||
library/configparser,,:option,${section:option} | ||
library/configparser,,:path,python_dir: ${Frameworks:path}/Python/Versions/${Frameworks:Python} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2020-05-31-10-48-47.bpo-4356.P8kXqp.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a key function to the bisect module. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 find suspicious that
key(x)
is called here and that it's not handled bybisect_right()
, e.g. I would have expected this to work:Is this on purpose?
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 recall the discussion in the other PR whether the "searched for item" should be an argument to
key
or a value produced bykey
.It's a bit clearer in the case of
insort_*
functions because array member is needed for insertion, thus, key will be applied to this value, thus the latter above.Perhaps it's easier to disambiguate in the docs by mentioning how the
key
will be used, and/or talk about type or shape of key and array members?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 think the API is correct in that it supports typical usage patterns. insort() will insert entire records and bisect() will scan the key field for the first match in a range at or above the key value.
In SQL, the pattern would look like this:
In Python, the example would look like this:
If bisect() required an entire record as an input, it would preclude the ability to do searches like like this.
Here's another example that we would want to support:
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'll echo what Raymond explained. It would be more useful to a library like sortedcontainers if the argument to bisect was the field value rather than the record. After all, the key function can be applied to the record to get the field value if need be. And there are many cases where constructing the record may be expensive or impossible just to determine rank within a list. The field value API is compatible with having the record and key function but not the other way around.
The SortedKeyList data type provides both
bisect_left
andbisect_key_left
variants. Thebisect_left
variant simply applies the key function and callsbisect_key_left
so I think the API specifying the field value is more generically useful.