Package: 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,pyintervalsis 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 makes 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,pyintervalsdefines intervals with date and time.
However, adding support for other comparable types such asint,floatis also possible.How?DeclareIntervalobjects withpyintervalsand check whether theyoverlapwith each other or
onecontainsthe other.frompyintervalsimportInterval,overlaps,containsfromdatetimeimportdatetimemy_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)>>>Truemy_first_interval.overlaps_with(my_second_interval)>>>Truecontains(my_first_interval,my_second_interval)>>>Truemy_first_interval.contains(my_second_interval)>>>Truemy_third_interval=Interval(start=datetime(1988,5,21,10,45),end=datetime(1989,6,20,1,30))overlaps(my_first_interval,my_third_interval)>>>Falsecontains(my_first_interval,my_third_interval)>>>Falsepyintervalsalso supportdegenerateintervals, which have theirstartequal to theirend.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)>>>Truemy_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)>>>TrueWhat else?Interval concept also leads toaggregate 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 programatically check whether the shop is open at a given timeT, then
you need to iterate overall (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 takeO(n)time.Linear time is nice but can we not improve it? Well, withpyintervals, you can!(with an upcoming feature)What we essentially are curious about is the status of that beautiful store at a given time.pintervalswillallow you fetch this value inO(log n)time.Seeroadmapfor the list of available and upcoming features.When?Start withpyintervalsright away withpipinstallpyintervalsRoadmappyintervalsis in active development and not feature complete yet. Please see below
for completed and planned features.Features:✅ = implemented, 🚧 = planned, ❌ = not plannedFundamentals:✅ Overlap controls✅ Contain controlsInterval Handler:🚧 Own intervals with associated values🚧 Provide value projection graph🚧 Query value over time🚧 Access intervals overlapping with a specific timespanSingle-level Pegging:🚧 Introduce object association to Intervals🚧 Single level pegging with first-in-first-out🚧 Enable callback for pegging quantity🚧 Enable callback for pegging matchingSupport other comparable types🚧 Define comparable protocol and generics🚧 Adapt Interval and Interval Handler conceptsAcknowledgementsFollowing resources and people have inspiredpyintervals:Always use [closed, open) intervalsArie Bovenbergpdfje (for initial setup of this project)Sam de WringerTim Lamballais-Tessensohn