Skip to main content

An intervals library implemented in pure python, meant to be a drop-in replacment for the pyinterval library.

Project description

Extents - An Intervals Library in Pure Python

extents is an intervals library implemented in pure python. It draws its inspiration from the PyInterval library (github).

This library is capable of representing open, closed and semi-open/closed intervals. It also supports set operations on intervals (i.e., union, intersection and complement).

Examples:

Basic use cases:

Basic use of this library includes creating simple intervals, and manipulating them through set operations

from extents import interval        # the interval class represents a collection of intervals

ival1 = interval([0, 1])            # the singleton interval set containing the closed interval [0, 1]
print(ival1)                        # Interval([0, 1])
print(~ival1)                       # Interval((-inf, 0), (1, inf)) -- the interval set of (-inf, 0) and (1, inf) -- 
                                    # which compose the complement of [0, 1]

ival2 = interval((4, 5))            # the singleton interval set containing the open interval (4, 5)
print(ival2)                        # Interval((4, 5))
print(~ival2)                       # Interval((-inf, 4], [5, inf)) -- the interval set of (-inf, 4] and [5, inf) --
                                    # which are the complement of (4, 5)]

ival3 = interval([0, 1], [5, 6])    # the interval set [0, 1] and [5, 6]
ival4 = interval([0, 3], [4, 5.5])  # the interval set [0, 3] and [4, 5.5]
print(ival3 | ival4)                # the interval set [0, 3] and [4, 6], which is the union of ival3 and ival4
print(ival3 & ival4)                # the interval set [0, 1] and [5, 5.5], which is the intersection of ival3 and ival4

ival5 = interval([0, 1], [0.5, 3])  # intervals are automatically union-ed during construction.
print(ival5)                        # resulting in the interval [0, 3]

The output of the commands above would be:

Interval([0, 1])
Interval((-inf, 0), (1, inf))
Interval((4, 5))
Interval((-inf, 4], [5, inf))
Interval([0, 3], [4, 6])
Interval([0, 1], [5, 5.5])
Interval([0, 3])

Advanced use cases:

In order to construct a semi-open interval, you can import the Component and ComponentType, and construct the internal interval components.

For example,

from extents import interval, Component, ComponentType

ival = interval(Component(0, 1, ComponentType.HALF_CLOSED_LEFT),
                Component(5, 6, ComponentType.HALF_CLOSED_RIGHT)
                )
print(ival)  # construct the interval set: [0, 1), (5, 6]

Additional Examples:

See the tests/extents_test.py file.

Installation:

From github:

$ pip install git+https://github.com/swapp-ai/extents

From source:

$ git clone https://github.com/swapp-ai/extents
$ cd extents
$ pip install .

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

extents-0.2.2.tar.gz (13.5 kB view hashes)

Uploaded Source

Built Distribution

extents-0.2.2-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

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