Skip to main content

⟨ beset ⟩

immutable, typed intervals with the interface of Python sets

  • 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.1

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.1
File Size Uploaded
beset-0.1.1.tar.gz 9.6 kB Details

Built distribution (wheel)

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

Total release size: 20.6 kB

Release files / beset-0.1.1.tar.gz

Download URL beset-0.1.1.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2ba6c4921e5cc1c611626ff173c4bd032ddcb335da8cc0e871271dea77b3a314
BLAKE2b-256 checksum
How to use checksums
e2437b777eab36b53a1f5be7217a4d1c8b8bbe3d39209ea5fc80919584d6fcff
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.1-py3-none-any.whl

Download URL beset-0.1.1-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63fd038f1609ef8aa7263ed6ca8ade871557b189527711d3e55ba12225cc871f
BLAKE2b-256 checksum
How to use checksums
23a47fcc1a625a5e43a51a5a6ab50768a9e4b5403f45b6eb70eef4784960eaa4
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

0.1.2

2 release files

This release

0.1.1 This release

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