segment

class igwn_segments.segment(iterable=(), /)

Bases: tuple

The segment class defines objects that represent a range of values. A segment has a start and an end, and is taken to represent the range of values in the semi-open interval [start, end). Some limited arithmetic operations are possible with segments, but because the set of (single) segments is not closed under sensible definitions of the standard arithmetic operations, the behaviour of the arithmetic operators on segments might not be as you would expect. For general arithmetic on segments, use segmentlist objects. The methods for this class exist mostly for purpose of simplifying the implementation of the segmentlist class.

The segment class is a subclass of the tuple built-in class provided by Python. This means segments are immutable: you cannot modify a segment object after creating it, to change the boundaries of a segment you must create a new segment object with the desired boundaries. Like tuples, segments can be used as dictionary keys, and like tuples the comparison used to find a segment in the dictionary is done by value not by ID. And, like tuples, a segment can be created from any sequence-like object by passing it to the constructor, however the sequence must have exactly two elements in it.

Example:

>>> segment(0, 10) & segment(5, 15)
segment(5, 10)
>>> segment(0, 10) | segment(5, 15)
segment(0, 15)
>>> segment(0, 10) - segment(5, 15)
segment(0, 5)
>>> segment(0, 10) < segment(5, 15)
True
>>> segment(1, 2) in segment(0, 10)
True
>>> segment(1, 11) in segment(0, 10)
False
>>> segment(0, 1)
segment(0, 1)
>>> segment(1, 0)
segment(0, 1)
>>> bool(segment(0, 1))
True
>>> bool(segment(0, 0))
False
>>> segment("AAA Towing", "York University") & segment("Pool", "Zoo")
segment('Pool', 'York University')
>>> x = [0, 1]  # a list
>>> y = segment(x)
>>> y
segment(0, 1)
>>> y == x
False
>>> y is x
False
>>> x in y
True
>>> z = {y: ["/path/to/file1", "/path/to/file2"]}
>>> y in z
True
>>> z[y]
['/path/to/file1', '/path/to/file2']

Methods Summary

connects(object, /)

Returns True if ths segment and other touch but do not overlap

contract(object, /)

Return a new segment whose bounds are given by adding x to the segment's lower bound and subtracting x from the segment's upper bound.

disjoint(object, /)

Returns >0 if self covers an interval above other's interval, <0 if self covers an interval below other's, or 0 if the two intervals are not disjoint (intersect or touch).

intersects(object, /)

Return True if the intersection of self and other is not a null segment.

protract(object, /)

Return a new segment whose bounds are given by subtracting x from the segment's lower bound and adding x to the segment's upper bound.

shift(object, /)

Return a new segment whose bounds are given by adding x to the segment's upper and lower bounds.

Methods Documentation

connects(object, /)

Returns True if ths segment and other touch but do not overlap

contract(object, /)

Return a new segment whose bounds are given by adding x to the segment’s lower bound and subtracting x from the segment’s upper bound.

disjoint(object, /)

Returns >0 if self covers an interval above other’s interval, <0 if self covers an interval below other’s, or 0 if the two intervals are not disjoint (intersect or touch). A return value of 0 indicates the two segments would coalesce.

intersects(object, /)

Return True if the intersection of self and other is not a null segment.

protract(object, /)

Return a new segment whose bounds are given by subtracting x from the segment’s lower bound and adding x to the segment’s upper bound.

shift(object, /)

Return a new segment whose bounds are given by adding x to the segment’s upper and lower bounds.