Closed padraic-shafer closed 5 months ago
@ambarb I believe this fixes the issue that you observed
Rebased this PR onto main to simplify the PR review. No other changes since previous review.
I don't understand why we have to check the value of the int:
python
Python 3.11.8 (main, Feb 12 2024, 14:50:05) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> int(2**456)
186070713419675363980626894819329160794532188335953423432061490990243657757029868371504908982723472783555205531204141550984858016925351936
>>>
Python's native int does not have a fixed bit length (it is one of the BigInt versions...but I don't remember off the top of my head how it works).
Python's native int does not have a fixed bit length (it is one of the BigInt versions...but I don't remember off the top of my head how it works).
You make a good point. I thought I had a good reason for this, but some changes in the approach were made since then. I'll go back and try without size cutoff under the current scenario.
@tacaswell Of course you are right about the variable-length int in python 3. This makes this PR vastly simpler.
I've added a separate test to distinguish between key values that are large vs. out-of-range for the catalog (as opposed to overflow of the binary int representation).
I'll go back and update the PR description above.
After the latest updates: Net changes to databroker code are very minimal now. Test cases have been expanded.
Description
Databroker will now accept a
numpy.int64
value as a key for accessing scans in a databroker catalog,as long as that value can be safely converted to a python int (int32). Otherwise aBehavior of using the int-valued key is unchanged by this PR: The key is treated as aKeyError
is raised.scan_id
value if positive, or theN
th-most recent scan index if negative. Suitable unit tests have been added to verify this.Motivation and Context
Databroker is frequently used in conjunction with pandas. By default pandas imports integer columns as
numpy.int64
; databroker then raises an exception when someone tries to use the int64-valuedscan_id
as a catalog key.Although type conversion could be done manually by the calling code, this is such a common scenario that it would improve out-of-the-box user experience to handle this case within databroker.
### Note: Stacked PRThis PR builds upon outstanding PR https://github.com/bluesky/databroker/pull/803. Please wait until https://github.com/bluesky/databroker/pull/803 has been accepted before merging this PR.How Has This Been Tested?
Passes linter and unit tests in GH actions of forked repo.
EDITED to clarify the intention that the int64-valued key be treated as an int-valued key; other than that, databroker behaves no differently.