Skip to main content

Efficient interval operations.

Project description

https://img.shields.io/pypi/v/pyintervals.svg?style=flat-square&color=blue https://img.shields.io/pypi/pyversions/pyintervals.svg?style=flat-square https://img.shields.io/pypi/l/pyintervals.svg?style=flat-square&color=blue https://img.shields.io/badge/mypy-strict-forestgreen?style=flat-square https://img.shields.io/badge/coverage-99%25-forestgreen?style=flat-square https://img.shields.io/github/actions/workflow/status/serkankalay/pyintervals/tests.yml?branch=master&style=flat-square

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 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, 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 support 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

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 programatically 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. pintervals will allow you fetch this value in O(log n) time.

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

  • 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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyintervals-1.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

pyintervals-1.0.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file pyintervals-1.0.1.tar.gz.

File metadata

  • Download URL: pyintervals-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pyintervals-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c55cdffcb5cf7247901448bfc93a4fa75e36b0aa064903eea1b5b101d2c9d419
MD5 2383d7719c07703c292b90c69751a00e
BLAKE2b-256 972e53625711e17322e09a4ab8cdac551fa26f1d14123b18bf6c1ee855d49843

See more details on using hashes here.

File details

Details for the file pyintervals-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyintervals-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pyintervals-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b994d83bcfa739769da2f290aa1a0ff37ca4129bb937d11e504a7f6daea4c54
MD5 27af6f802cf3deb4cefac65d0e1d3dbc
BLAKE2b-256 1ecb2e8e51bbbce9bd359d843a16e24785b08dcf780856eaf72d0cc5355eb9f6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page