Skip to main content

⟨ beset ⟩

immutable, typed intervals with the interface of Python sets

PyPI Python versions License Build

  • Intervals are typed as generics and pass type checks by all common Python type checkers:
    • mypy --strict
    • ty
    • pyright
    • pyrefly
  • Intervals are immutable and hashable
  • Intervals behave like sets
    • If you know Python set operations, you know how to use this library
  • Intervals can be used on any data type that supports <, the less-than operator, for linear ordering
    • Typical: int, float, datetime
    • Even a class like str with no notion of distance between values, can be used for intervals (e.g. dictionary ranges)

Examples

Intervals are sets that contain all possible values between their lower and upper bounds. The Closed interval also includes the bounds themselves.

>>> from beset import Closed

>>> x = Closed(1, 3)

>>> print(x)
[1 ; 3]

>>> 0 in x, 1 in x, 2 in x, 3 in x, 4 in x
(False, True, True, True, False)

>>> x.start, x.stop
(1, 3)

The half-open ClosedOpen interval includes its lower bound but not its upper one.

>>> from beset import ClosedOpen

>>> y = ClosedOpen(1, 3)

>>> print(y)
[1 ; 3)

>>> 2 in y, 3 in y, 4 in y
(True, False, False)

The Open interval excludes both its bounds and the OpenClosed interval includes its upper bound, but not its lower one.

You can also create the required interval dynamically using their Interval base class.

>>> from beset import Interval

>>> Interval(9, 99, start_closed=False, stop_closed=True)
OpenClosed(9, 99)

Intervals support all Python set operations. Some examples:

>>> ClosedOpen(10, 20) & ClosedOpen(15, 25)  # intersection
ClosedOpen(15, 20)

>>> ClosedOpen(3, 9) < Open(0, 10)  # is proper subset
True

Set subtraction can lead to disjoint sets. The beset library represents these using the class IntervalSet.

>>> s = Open(0, 10) - Open(3, 5)

>>> s
IntervalSet([OpenClosed(0, 3), ClosedOpen(5, 10)])

>>> print(s)
(0 ; 3] | [5 ; 10)

You can also create an IntervalSet explicitly, but it's often easier to use the union operator on simple intervals. The results are equal.

>>> IntervalSet([Open(10, 20), Open(30, 40)]) == Open(10, 20) | Open(30, 40)
True

The beset library supports unbounded intervals without upper or lower bound. Create such intervals by using None as a bound.

>>> x = Closed(10, None)
>>> print(x)
[10 ; +inf⟩
>>> 100 in x
True

Unbounded intervals allow for the introduction of the complement operation that returns the complementary interval, containing everything not in the original interval.

>>> Closed(-3, 7).complement()
IntervalSet([Open(None, -3), Open(7, None)])

>>> print(~ClosedOpen(0, 100))  # the ~-operator returns the complement
⟨-inf ; 0) | [100 ; +inf⟩

Typing

The Interval and IntervalSet class are generics. Type checkers automatically infer the correct type.

>>> reveal_type(ClosedOpen(2.718, 6.283))  # Revealed type is beset.ClosedOpen[float]

Taking the complement of an interval can introduce None values.

>>> print(x := ~Closed(0, 10))
⟨-inf ; 0) | (10 ; +inf⟩

>>> reveal_type(x)  # Revealed type is beset.IntervalSet[int | None]

Getting rid of the union with None can be accomplished using intersection.

>>> domain = Closed(-100, 100)
>>> y = domain & x
>>> print(y)
[-100 ; 0) | (10 ; 100]

>>> reveal_type(y)  # Revealed type is beset.IntervalSet[int]

Metadata

Release files for beset 0.1.2

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

Source distribution (sdist)

Source distribution for beset 0.1.2
File Size Uploaded
beset-0.1.2.tar.gz 10.5 kB Details

Built distribution (wheel)

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

Total release size: 22.6 kB

Release files / beset-0.1.2.tar.gz

Download URL beset-0.1.2.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
47a03f1e9b4c01d711b180d615758a01b9fc0aa451bf4fee6ef62fbde42b0b67
BLAKE2b-256 checksum
How to use checksums
45294b242b226713d9c2a5425b414ba4a48ffeadd6f88af631838cb78865f058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / beset-0.1.2-py3-none-any.whl

Download URL beset-0.1.2-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a821fc3f961fa560e267874ff871bddeae6ee4d200359a714f0b29a4001c2e39
BLAKE2b-256 checksum
How to use checksums
b73de167e8c50ab8d3f4feb711633d8b8e184794d0c5a68210397af7d012e6e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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