📐 pyintervals
Execute efficient interval operations in Python.
(Currently in active development. Leave a ⭐️ on GitHub if you’re interested how this develops!)
Why?
Inspired by a discussion and initial implementation in a professional project and a library I’ve been using in one of my previous jobs, pyintervals is born.
Intervals pop up frequently in programming, specifically in domains where you have an activity or a proxy for it.
Suppose you are implementing a single machine scheduling algorithm. In order to schedule an operation, you need to make sure that the machine is available during your desired time of operation.
Or you are implementing a booking system and need to check that the hotel has at least 1 room with desired number of beds for the dates selected. For such cases, you need to control some information overlapping with an interval.
As the examples suggest, pyintervals defines intervals with date and time. However, adding support for other comparable types such as int, float is also possible.
How?
Declare Interval objects with pyintervals and check whether they overlap with each other or one contains the other.
from pyintervals import Interval, overlaps, contains
from datetime import datetime
my_first_interval = Interval(start=datetime(2017,5,20,12,15),end=datetime(2024,10,10,19,0))
my_second_interval = Interval(start=datetime(2024,10,6,7,21),end=datetime(2024,10,10,19,0))
overlaps(my_first_interval, my_second_interval)
>>> True
my_first_interval.overlaps_with(my_second_interval)
>>> True
contains(my_first_interval, my_second_interval)
>>> True
my_first_interval.contains(my_second_interval)
>>> True
my_third_interval=Interval(start=datetime(1988,5,21,10,45),end=datetime(1989,6,20,1,30))
overlaps(my_first_interval,my_third_interval)
>>> False
contains(my_first_interval,my_third_interval)
>>> False
pyintervals also supports degenerate intervals, which have their start equal to their end.
my_degenerate_interval = Interval(start=datetime(2024,10,10,9,0), end=datetime(2024,10,10,9,0))
overlaps(my_first_interval, my_degenerate_interval)
>>> True
my_same_degenerate_interval = Interval(start=datetime(2024,10,10,9,0), end=datetime(2024,10,10,9,0))
overlaps(my_degenerate_interval, my_same_degenerate_interval)
>>> True
Grouping connected items
Often you do not need a single interval but the clusters of items that belong together. Suppose a hotel wants to know which bookings form a continuous block of occupancy, so that the cleaning crew can be scheduled once per block. group_connected does exactly that: it takes your items and yields lists of the items that are connected.
Items are connected when their intervals overlap or touch. A booking ending at 12:00 and another starting at 12:00 belong to the same group, whereas a gap of any length separates groups.
from dataclasses import dataclass
from pyintervals import Interval, group_connected
@dataclass
class Booking:
guest: str
interval: Interval # Items expose their interval with this attribute
bookings = [
Booking("Ada", Interval(start=datetime(2025,7,21,9,0), end=datetime(2025,7,21,11,0))),
Booking("Bob", Interval(start=datetime(2025,7,21,10,0), end=datetime(2025,7,21,12,0))),
Booking("Cem", Interval(start=datetime(2025,7,21,12,0), end=datetime(2025,7,21,13,0))),
Booking("Dan", Interval(start=datetime(2025,7,21,15,0), end=datetime(2025,7,21,16,0))),
]
[[booking.guest for booking in group] for group in group_connected(bookings)]
>>> [['Ada', 'Bob', 'Cem'], ['Dan']]
Ada and Bob overlap, and Cem starts right when Bob leaves, so the three form one group. Dan comes after a gap and stands alone.
If your items do not have an interval attribute, tell group_connected how to find it with key. Items can be given in any order; groups are yielded in start order and items within a group are ordered by start, with ties kept in input order.
group_connected(reversed(bookings), key=lambda booking: booking.interval)
If your items are already ordered by start, pass assume_sorted=True to stream the groups lazily. A violation of the order raises a ValueError as soon as it is encountered.
Degenerate intervals have no timespan, so they are dropped and never appear in a group.
What else?
Interval concept also leads to aggregate value over time. Let’s dive with an example:
Let there be a beautiful and exclusive patisserie and you heard it from a foodie friend. She/he suggested you to go there as soon as possible. You checked your agenda and seems you have an empty spot at your calendar starting at 12:30. The place is open between 9:00-12:00 and 13:00 - 16:00 daily.
If you want to programmatically check whether the shop is open at a given time T, then you need to iterate over all (in the worst case) the time intervals the patisserie is open for the time you are curious about, 12:30 in this case. This will take O(n) time.
Linear time is nice but can we not improve it? Well, with pyintervals, you can! What we essentially are curious about is the status of that beautiful store at a given time. pyintervals allows you to fetch this value in O(log n) time.
# Add open times with value 1
mon_interval_1 = Interval(start=datetime(2025,7,21,9,00),end=datetime(2025,7,21,12,0), value=1)
mon_interval_2 = Interval(start=datetime(2025,7,21,13,00),end=datetime(2025,7,21,16,0), value=1)
tue_interval_1 = Interval(start=datetime(2025,7,22,9,00),end=datetime(2025,7,22,12,0), value=1)
tue_interval_2 = Interval(start=datetime(2025,7,22,13,00),end=datetime(2025,7,22,16,0), value=1)
capacity = IntervalHandler()
capacity.add(
[
mon_interval_1,
mon_interval_2,
tue_interval_1,
tue_interval_2,
]
)
# Check the capacity, which indicates open when value is positive
capacity.value_at_time(datetime(2025,7,21,9,30))
>>> 1 # Open
capacity.value_at_time(datetime(2025,7,21,12,30))
>>> 0 # Closed
capacity.value_at_time(datetime(2025,7,22,13,00))
>>> 1 # Open
capacity.value_at_time(datetime(2025,7,22,16,00))
>>> 0 # Closed
capacity.value_at_time(datetime(2025,7,22,15,59))
>>> 1 # Open
See roadmap for the list of available and upcoming features.
When?
Start with pyintervals right away with
pip install pyintervals
Roadmap
pyintervals is in active development and not feature complete yet. Please see below for completed and planned features.
Features:
✅ = implemented, 🚧 = planned, ❌ = not planned
- Fundamentals:
✅ Overlap controls
✅ Contain controls
✅ Grouping connected objects with intervals
- Interval Handler:
✅ Own intervals with associated values
✅ Provide value projection graph
✅ Query value over time
🚧 Access intervals overlapping with a specific timespan
- Single-level Pegging:
🚧 Introduce object association to Intervals
🚧 Single level pegging with first-in-first-out
🚧 Enable callback for pegging quantity
🚧 Enable callback for pegging matching
- Support other comparable types
🚧 Define comparable protocol and generics
🚧 Adapt Interval and Interval Handler concepts
Acknowledgements
Following resources and people have inspired pyintervals:
Tim Lamballais-Tessensohn
Metadata
Release files for pyintervals 1.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyintervals-1.7.0.tar.gz | 12.8 kB | Without provenance | Details for pyintervals-1.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyintervals-1.7.0-py3-none-any.whl | Python 3 | none | any | Details for pyintervals-1.7.0-py3-none-any.whl |
Total release size: 25.1 kB
Release files / pyintervals-1.7.0.tar.gz
| Download URL | pyintervals-1.7.0.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb11c365c8987afff0b39bfda3e0903848aab2864a66c53e1ab31fc584832655
|
|
BLAKE2b-256 checksum How to use checksums |
d646903ca5a1b00978f5eaab3abf7a5f2b0d022f403b007a5806fae91c31f38c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.7
|
Release files / pyintervals-1.7.0-py3-none-any.whl
| Download URL | pyintervals-1.7.0-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fdc2eb9c32cc0ce8c0f14a37003f053b6d6f7491e7c79bc30c79361853480a0
|
|
BLAKE2b-256 checksum How to use checksums |
8e9a7ce910cd1bc2c44547cd89790c179103d63c10a70562983570abbab4cded
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.7
|
Security for pyintervals 1.7.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.