Skip to main content

jbisect

Reusable implementation of the bisect / binary search algorithm.

This (obviously) competes with the standard library bisect package. Whereas bisect only searches lists this package supports searching on a function, and supports both integer and floating-point indices.

Install with:

pip install jbisect

Basic searching

jbisect provides the function search_seq for searching sequences:

from jbisect import search_seq

print(search_seq("011222355", "2"))

By default the entire sequence is searched, but you can use the parameters low and high to limit the search range:

print(search_seq("011222355", "2", low=1, high=5))

You can use the side parameters to configure whether to return the first match, or just past the last match:

print(search_seq("011222355", "2", side="right"))

If you have a sequence that is descending, instead of ascending, you need to set the ordering parameter:

print(search_seq("553222110", "2", ordering="descending"))

Searching functions:

The functions search_int_fn and search_float_fn can be used to search a function instead of a sequence. These functions take the same low, high, side and ordering arguments as search_seq.

from jbisect import search_int_fn, search_float_fn

print(search_int_fn(lambda i: i * i, 16, low=0))
print(search_float_fn(lambda i: i * i, 2.0, low=0.0))

Searching predicates:

Finally the functions search_int_pred and search_float_pred can be used to find the first value accepted by a predicate. pred must be a function that returns a bool, and for which there exists some x so that for all y<x pred(y) is False; and for all y>=x pred(y) is True. search_*_pred will then find x.

from jbisect import search_int_pred, search_float_pred

print(search_int_pred(lambda i: i * i >= 16, low=0))
print(search_float_pred(lambda i: i * i >= 2.0, low=0.0))

NumPy support:

The package jbisect.numpy adds functionality for searching NumPy arrays and functions. Again, this obviously competes with the NumPy function searchsorted. However, searchsorted is limited in that it only searches existing arrays, and not functions.

jbisect.numpy provides three functions search_numpy_array, search_numpy_fn and search_numpy_pred, mirroring the pure-python functions above. In the case of NumPy, we do not need to distinguish between int and float up-front, as this is determined by the dtype.

search_numpy_array takes the new argument axis that determines which axis of the input array to search.

search_numpy_fn and search_numpy_pred takes the new arguments dtype and shape which determines the dtype and shape of the input to the function/predicate, and the return type of the function.

import numpy as np

from jbisect.numpy import search_numpy_array, search_numpy_fn, search_numpy_pred

print(search_numpy_array([0, 1, 1, 2, 2, 2, 3, 5, 5], 2))

print(
    search_numpy_array(
        [
            [
                [111, 112, 113, 114],
                [121, 122, 123, 124],
                [131, 132, 133, 134],
            ],
            [
                [211, 212, 213, 214],
                [221, 222, 223, 224],
                [231, 232, 233, 234],
            ],
        ],
        [[122], [223]],
        axis=1,
    )
)

print(search_numpy_fn(lambda i: i * i, 16, low=0, high=1000, shape=(), dtype=np.int64))

print(search_numpy_pred(lambda i: i >= 4, shape=[], dtype=np.int64))

Metadata

Release files for jbisect 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jbisect 0.4.0
File Size Uploaded Provenance Actions
jbisect-0.4.0.tar.gz 9.8 kB Without provenance Details for jbisect-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jbisect 0.4.0
File Interpreter ABI Platform Actions
jbisect-0.4.0-py3-none-any.whl Python 3 none any Details for jbisect-0.4.0-py3-none-any.whl

Total release size: 19.4 kB

Release files / jbisect-0.4.0.tar.gz

Download URL jbisect-0.4.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cd521f3669b115af4d3832ae973537fee663c571ee7d3b267856dd70ffdb60ba
BLAKE2b-256 checksum
How to use checksums
14c0e81fe00efa1065cf5cfbb96f72f84fe3b3802d3d55211424a8aff57e5267
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / jbisect-0.4.0-py3-none-any.whl

Download URL jbisect-0.4.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08428b5729e9d3ff045660cbc9ab96673577baf41a6004a1012558af55b9eeb6
BLAKE2b-256 checksum
How to use checksums
fc87c3049f61579f831ff0b48d3e3d3cea422e438db7cfd57449d4b208006375
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Security for jbisect 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page