Skip to main content

Powerful Interval Set operations.

Project description

Install

pip install intervalset2

Basic Use Case

  • import
>> > from src.intervalset2 import IntervalSet, NEG_INF, POS_INF
  • initialize an interval set (follow simply called "TVS")
>>> IntervalSet.parse_from_str('(-inf, -10) | (-3, 3) | 5 | (10, inf)')
(-inf, -10) | (-3, 3) | 5 | (10, inf)       # a TVS all intervals is open
>>> IntervalSet.parse_from_str('(-inf, -10) | [-3, 3) | 5 | [10, inf)')
(-inf, -10) | [-3, 3) | 5 | [10, inf)       # a TVS part of intervals is open
>>> IntervalSet.parse_from_str('(-inf, -10] | [-3, 3] | 5 | [10, inf)')
(-inf, -10] | [-3, 3] | 5 | [10, inf)       # a TVS all intervals is close
>>> IntervalSet.parse_from_str('')          # an empty TVS

>>> IntervalSet.empty_tvs()                 # get an empty TVS

>>> IntervalSet.inf_tvs()                   # get an infinite TVS
(-inf, inf)
>>> tvs = IntervalSet.parse_from_str('(2, 5) | 5 | (5, 8) | (7, 9) | (10, inf)')
>>> tvs                                     # intervals will be merged automatically
(2, 9) | (10, inf)  
>>> len(tvs)                                # the number of its intervals
2  
>>> tvs[1]                                  # TVS support __getitem__
(10, inf)  
>>> tvs[:]                                  # TVS support slice __getitem__
[(2, 9), (10, inf)]  
>>> [2, 3, False, True] in tvs              # TVS support __contains__
True 
>>> 20 in tvs                               # TVS support __contains__ on  discrete point
True 
>>> tvs2 = tvs.copy()                       # get a copy
>>> tvs2 == tvs  
True                                        # all intervals of two TVS objects is same
>>> tvs2.is_disjoint(tvs)                   # whether a TVS overlap on other
False                                       
>>> from datetime import date
>>> IntervalSet([[date.fromisoformat('2023-12-03'), 
                  date.fromisoformat('2023-12-05'), 
                  True, False]])
[2023-12-03, 2023-12-05)                    # also support other data type as interval 
>>> IntervalSet([['a', 'c', True, False],['b', 'd', True, False]])
[a, d)
  • add an interval into a tvs
>>> tvs1 = IntervalSet.parse_from_str('(0, 3] | (100, inf)')
>>> tvs1.add(3, 4)
>>> tvs1
(0, 4) | (100, inf)
>>> tvs1.add(4, 4)
>>> tvs1
(0, 4] | (100, inf)
  • union
>>> tvs1 = IntervalSet.parse_from_str('(0, 3] | (100, inf)')
>>> tvs2 = IntervalSet.parse_from_str('(2, 5) | (-inf, -100)')
>>> tvs3 = IntervalSet.parse_from_str('[4, 9) | 0')
>>> tvs1.union(tvs2, tvs3)
(-inf, -100) | [0, 9) | (100, inf)
>>> tvs1.union()  # also can passed in zero other interval sets
(0, 3] | (100, inf)
  • intersection
>>> tvs1 = IntervalSet.parse_from_str('(0, 5] | (100, inf)')
>>> tvs2 = IntervalSet.parse_from_str('(2, 7) | (200, inf)')
>>> tvs3 = IntervalSet.parse_from_str('[4, 9) | (300, inf)')
>>> tvs1.intersection(tvs2, tvs3)
[4, 5] | (300, inf)
>>> tvs1.intersection()  # also can passed in zero other interval sets
(0, 5] | (100, inf)
  • difference
>>> tvs1 = IntervalSet.parse_from_str('(0, 10) | (100, inf)')
>>> tvs2 = IntervalSet.parse_from_str('(2, 7) | (200, inf)')
>>> tvs3 = IntervalSet.parse_from_str('[4, 9) | (300, inf)')
>>> tvs1.difference(tvs2, tvs3)
(0, 2] | [9, 10) | (100, 200]
>>> tvs1.intersection()  # also can passed in zero other interval sets
(0, 10) | (100, inf)
  • symmetric difference
>>> tvs1 = IntervalSet.parse_from_str('(0, 3] | (100, inf)')
>>> tvs2 = IntervalSet.parse_from_str('(2, 5) | (200, inf)')
>>> tvs3 = IntervalSet.parse_from_str('[4, 9) | (300, inf)')
>>> tvs1.symmetric_difference(tvs2, tvs3)
(0, 2] | (3, 4) | [5, 9) | (100, 200]
>>> tvs1.symmetric_difference()  # also can passed in zero other interval sets
(0, 3] | (100, inf) 
  • complementary
>>> tvs1 = IntervalSet.parse_from_str('(0, 3] | (100, inf)')
>>> tvs1.complementary()
(-inf, 0] | (3, 100]
>>> tvs1 = IntervalSet.parse_from_str('-3 | (0, 2]')
>>> tvs2 = IntervalSet.parse_from_str('(3, 5)')
>>> tvs3 = IntervalSet.parse_from_str('[7, 9) | (300, inf)')
>>> IntervalSet.complementary_many(tvs1, tvs2, tvs3)
(-inf, -3) | (-3, 0] | (2, 3] | [5, 7) | [9, 300]
  • other
    • union also support | |=
    • intersection also support & &=
    • difference also support - -=
    • symmetric difference also support ^ ^=
    • complementary also support ~

FAQ

  • for interval in a TVS, what required?
    • must support compare methods, like lt, gt, ...
  • follow-up plan?
    • improve the performance of some methods

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

intervalset2-1.0.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

intervalset2-1.0.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: intervalset2-1.0.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for intervalset2-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5849ebef53d59984b7a0591cdc0fabffd68e21feea646b8533c3f96eeff4206b
MD5 2e3aa629bf691086b3bda9ce4bc25f69
BLAKE2b-256 f848044c21c3bc01d6408573ff156c5262692ca0814ca1493910818ebd777a1e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: intervalset2-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for intervalset2-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 71d1bff786da8ecb27f2b60573adc0b5bca573fcb42f2823d10d8c44f31892d4
MD5 2dfb8f72e6fb40b326123579b98d7295
BLAKE2b-256 148eb744bc1e9c56b0b94b1b824ea49df0749fdd2dd6ac48f01266d8a1887645

See more details on using hashes here.

Supported by

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