Skip to content

Commit

Permalink
Interval measure property: length of a numeric interval
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-ubskii committed May 19, 2021
1 parent 119fe89 commit 19e9767
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions portion/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ def enclosure(self):
"""
return Interval.from_atomic(self.left, self.lower, self.upper, self.right)

@property
def measure(self):
"""
Return a measure (~ length) of the interval.
Only valid for intervals with numeric bounds.
"""
if self.empty:
return 0
if self.upper != inf and type(self.upper) not in (int, float):
raise TypeError("Interval bounds must be numeric")
if self.lower != -inf and type(self.lower) not in (int, float):
raise TypeError("Interval bounds must be numeric")
if self.upper == inf or self.lower == -inf:
return float("inf")
return sum([i.upper - i.lower for i in self._intervals])

def replace(
self, left=None, lower=None, upper=None, right=None, *, ignore_inf=True
):
Expand Down
25 changes: 25 additions & 0 deletions tests/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ def test_enclosure(self):
assert P.closed(0, 4) == (P.closed(0, 1) | P.closed(3, 4)).enclosure
assert P.openclosed(0, 4) == (P.open(0, 1) | P.closed(3, 4)).enclosure

def test_measure(self):
assert P.empty().measure == 0
assert P.singleton(1).measure == 0
assert (P.singleton(-1) | (P.singleton(0) | P.singleton(1))).measure == 0

assert P.open(0, 1).measure == 1
assert P.openclosed(0, 1).measure == 1
assert P.closedopen(0, 1).measure == 1
assert P.closed(0, 1).measure == 1

assert (P.open(0, 1) | P.open(3, 5)).measure == 3

assert (P.open(0, P.inf)).measure == float('inf')
assert (P.open(-P.inf, 0)).measure == float('inf')
assert (P.open(-P.inf, P.inf)).measure == float('inf')

with pytest.raises(TypeError):
P.open('a', 'b').measure

with pytest.raises(TypeError):
P.open('a', P.inf).measure

with pytest.raises(TypeError):
P.open(-P.inf, 'b').measure


class TestIntervalReplace:
def test_replace_bounds(self):
Expand Down

0 comments on commit 19e9767

Please sign in to comment.