Interval

class part.Interval(lower_value=None, upper_value=None, lower_closed=True, upper_closed=None)

Interval class.

The Interval class (which inherits from the Atomic class) is designed to hold range values. Allen’s interval algebra has been implemented.

An interval can hold any type of value that implements a total order.

Each bound of the interval can be open or closed.

static __new__(cls, lower_value=None, upper_value=None, lower_closed=True, upper_closed=None)

Create an Atomic instance.

Keyword Arguments
  • lower_value (TO, optional) – Any python comparable object.

  • upper_value (TO, optional) – Any python comparable object.

  • lower_closed (bool, optional) – A boolean value.

  • upper_closed (bool, optional) – A boolean value.

Returns

  • Interval – if the arguments define a non-empty interval.

  • Empty – otherwise.

Raises

ValueError – If lower_value is not comparable with upper_value.

See also

__init__

For interval initialization.

Examples

>>> from part import Interval
>>> a = Interval[int](lower_value=10, upper_value=0)
>>> bool(a)
False
__init__(lower_value=None, upper_value=None, lower_closed=True, upper_closed=None)

Initialize an Interval instance.

By default, an interval is closed to the left and open to the right.

Keyword Arguments
  • lower_value (TO, optional) – Any python object.

  • upper_value (TO, optional) – Any python object.

  • lower_closed (bool, optional) – A boolean value.

  • upper_closed (bool, optional) – A boolean value.

See also

__new__

For detection of empty interval creation.

Examples

>>> from part import Interval
>>> print(Interval[int](lower_value=10, upper_value=20))
[10;20)
>>> print(Interval[str](lower_value="abc", upper_value="def",
... upper_closed=True))
['abc';'def']
>>> print(Interval[int](lower_closed=None, lower_value=10, upper_value=20))
(10;20)
>>> print(Interval[int]())
(-inf;+inf)
__str__()

Return str(self).

__eq__(other)

Return self==other.

__lt__(other)

Comparison using Allen’s algebra.

Parameters

other (Atomic) – Another atomic value.

Returns

  • True – if the interval is lesser than the other.

  • False – otherwise.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a < Atomic[int].from_tuple((25, 30))
True
__gt__(other)

Comparison using Allen’s algebra.

Parameters

other (Atomic) – Another atomic value.

Returns

  • True – if the interval is greater than the other.

  • False – otherwise.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a > Atomic[int].from_tuple((25, 30))
False
__hash__()

Return hash(self).

__bool__()

Return bool(self).

Returns

An interval is always True.

Return type

True

__or__(other)

Compute the union of two intervals.

Parameters

other (Atomic) – Another atomic value.

Returns

The union of the interval and the other.

Return type

FrozenIntervalSet

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> b = Atomic[int].from_tuple((15, 30))
>>> c = Atomic[int].from_tuple((30, 40))
>>> print(a | b)
(10;30)
>>> print(a | c)
(10;20) | [30;40)
__and__(other)

Compute the intersection of two intervals.

Parameters

other (Atomic) – Another atomic value.

Returns

The intersection of the interval and the other.

Return type

FrozenIntervalSet

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> b = Atomic[int].from_tuple((15, 30))
>>> c = Atomic[int].from_tuple((30, 40))
>>> print(a & b)
[15;20)
>>> print(a & c)
__sub__(other)

Compute the difference of two intervals.

Parameters

other (Atomic) – Another atomic value.

Returns

The difference between the interval and the other.

Return type

FrozenIntervalSet

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> b = Atomic[int].from_tuple((15, 30))
>>> c = Atomic[int].from_tuple((30, 40))
>>> print(a - b)
(10;15)
>>> print(a - c)
(10;20)
__xor__(other)

Compute the symmetric difference of two intervals.

Parameters

other (Atomic) – Another atomic value.

Returns

The symmetric difference between the interval and the other.

Return type

FrozenIntervalSet

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> b = Atomic[int].from_tuple((15, 30))
>>> c = Atomic[int].from_tuple((30, 40))
>>> print(a ^ b)
(10;15) | [20;30)
>>> print(a ^ c)
(10;20) | [30;40)
__invert__()

Compute the complement of the interval.

Returns

The complement of the interval.

Return type

FrozenIntervalSet

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(~a)
(-inf;10] | [20;+inf)
static upper_limit(value=None, closed=None)

Create an interval from an upper limit.

Parameters
  • value (TO) – The upper limit.

  • closed (bool) – Is the interval closed?

Returns

An interval with an upper limit.

Return type

Interval

Examples

>>> from part import Interval
>>> print(Interval[int].upper_limit(value=10))
(-inf;10)
>>> print(Interval[int].upper_limit(value=10, closed=True))
(-inf;10]
static lower_limit(value=None, closed=True)

Create an interval from a lower limit.

Parameters
  • value (TO) – The lower limit.

  • closed (bool) – Is the interval closed?

Returns

An interval with a lower limit.

Return type

Interval

Examples

>>> from part import Interval
>>> print(Interval[int].lower_limit(value=10))
[10;+inf)
>>> print(Interval[int].lower_limit(value=10, closed=None))
(10;+inf)
before(other, strict=True, reverse=False)

Return True if the subset is the before the other.

Parameters
  • other (Atomic) – Another atomic object.

  • strict (bool) – is the before strict?

  • reverse (bool) – is the before reversed?

Raises

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a.before(Atomic[int].from_tuple((25, 30)))
True
meets(other, strict=True, reverse=False)

Return True if the subset meets the other.

Parameters
  • other (Atomic) – Another atomic object.

  • strict (bool) – is the meets strict?

  • reverse (bool) – is the meets reversed?

Raises

Examples

>>> from part import Atomic
>>> a = Atomic.from_tuple((10, 20, None))
>>> a.meets(Atomic[int].from_tuple((20, 30)))
False
>>> a.meets(Atomic[int].from_tuple((20, 30)), strict=False)
True
overlaps(other, strict=True, reverse=False)

Return True if the subset overlaps the other.

Parameters
  • other (Atomic) – Another atomic object.

  • strict (bool) – is the overlaps strict?

  • reverse (bool) – is the overlaps reversed?

Raises

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a.overlaps(Atomic[int].from_tuple((15, 30)))
True
starts(other, strict=True, reverse=False)

Return True if the subset starts the other.

Parameters
  • other (Atomic) – Another atomic object.

  • strict (bool) – is the starts strict?

  • reverse (bool) – is the starts reversed?

Raises

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a.starts(Atomic[int].from_tuple((20, 40, None)))
False
>>> a.starts(Atomic[int].from_tuple((10, 40, None)), strict=False)
True
during(other, strict=True, reverse=False)

Return True if the subset is during the other.

Parameters
  • other (Atomic) – Another atomic object.

  • strict (bool) – is the during strict?

  • reverse (bool) – is the during reversed?

Raises

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a.during(Atomic[int].from_tuple((0, 30)))
True
finishes(other, strict=True, reverse=False)

Return True if the subset finishes the other.

Parameters
  • other (Atomic) – Another atomic object.

  • strict (bool) – is the finishes strict?

  • reverse (bool) – is the finishes reversed?

Raises

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> a.finishes(Atomic[int].from_tuple((0, 20)))
True
property lower

Get the lower property.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(a.lower)
10+
property lower_value

Get the lower_value property.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(a.lower_value)
10
property lower_closed

Get the lower_closed property.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(a.lower_closed)
None
property upper

Get the upper property.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(a.upper)
20-
property upper_value

Get the lower_value property.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(a.upper_value)
20
property upper_closed

Get the upper_closed property.

Examples

>>> from part import Atomic
>>> a = Atomic[int].from_tuple((10, 20, None))
>>> print(a.upper_closed)
None