def test_overlaps(): # Overlaps should reject non supported types with pytest.raises(TypeError): I.closed(0, 1).to_atomic().overlaps(1) with pytest.raises(TypeError): I.closed(0, 1).overlaps(1) assert I.closed(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.open(0, 1)) assert I.open(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.openclosed(0, 1)) assert I.closed(0, 1).overlaps(I.closedopen(0, 1)) assert I.closed(1, 2).overlaps(I.closed(2, 3)) assert I.closed(1, 2).overlaps(I.closedopen(2, 3)) assert I.openclosed(1, 2).overlaps(I.closed(2, 3)) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3)) assert not I.closed(0, 1).overlaps(I.closed(3, 4)) assert not I.closed(3, 4).overlaps(I.closed(0, 1)) assert not I.closed(0, 1).overlaps(I.open(1, 2)) assert not I.closed(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closedopen(1, 2)) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.open(1, 2)) assert not I.open(0, 1).overlaps(I.open(1, 2)) assert not I.empty().overlaps(I.open(-I.inf, I.inf)) assert not I.open(-I.inf, I.inf).overlaps(I.empty())
def test_replace(): i = I.open(-I.inf, I.inf) assert i.replace(lower=lambda v: 1, upper=lambda v: 1) == I.open(-I.inf, I.inf) assert i.replace(lower=lambda v: 1, upper=lambda v: 2, ignore_inf=False) == I.open(1, 2) assert I.empty().replace(left=I.CLOSED, right=I.CLOSED) == I.empty() assert I.empty().replace(lower=1, upper=2) == I.open(1, 2) assert I.empty().replace(lower=lambda v: 1, upper=lambda v: 2) == I.empty() assert I.empty().replace(lower=lambda v: 1, upper=lambda v: 2, ignore_inf=False) == I.open(1, 2) i = I.closed(0, 1) | I.open(2, 3) assert i.replace() == i assert i.replace(I.OPEN, -1, 4, I.OPEN) == I.openclosed(-1, 1) | I.open(2, 4) assert i.replace(lower=2) == I.closedopen(2, 3) assert i.replace(upper=1) == I.closedopen(0, 1) assert i.replace(lower=5) == I.empty() assert i.replace(upper=-5) == I.empty() assert i.replace( left=lambda v: not v, lower=lambda v: v - 1, upper=lambda v: v + 1, right=lambda v: not v) == I.openclosed(-1, 1) | I.openclosed(2, 4) assert I.empty().replace(lower=2, upper=4) == I.open(2, 4)
def test_creation(): assert I.closed(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.CLOSED) assert I.open(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.OPEN) assert I.openclosed(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.CLOSED) assert I.closedopen(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.OPEN) assert I.closed(-I.inf, I.inf) == I.open(-I.inf, I.inf) assert I.singleton(2) == I.closed(2, 2) assert I.Interval() == I.open(0, 0) assert I.empty() == I.Interval() assert I.closed(3, -3) == I.empty() assert I.openclosed(3, 3) == I.empty() # I.empty() is a singleton assert I.empty() is I.empty() assert I.Interval(I.closed(0, 1).to_atomic()) == I.closed(0, 1) assert I.Interval(I.closed(0, 1)) == I.closed(0, 1) assert I.Interval(I.closed(0, 1).to_atomic(), I.closed(2, 3)) == I.closed(0, 1) | I.closed(2, 3) assert I.Interval(I.closed(0, 1) | I.closed(2, 3)) == I.closed(0, 1) | I.closed(2, 3) with pytest.raises(TypeError): I.Interval(1)
def test_from_string_customized(): i1, i2, i3, i4 = '<"0"-"1">', '<!"0"-"1">', '<"0"-"1"!>', '<!"0"-"1"!>' params = { 'conv': lambda s: int(s[1:-1]), 'disj': ' or ', 'sep': '-', 'left_open': '<!', 'left_closed': '<', 'right_open': '!>', 'right_closed': '>', 'pinf': r'\+oo', 'ninf': '-oo', } assert I.from_string(i1, **params) == I.closed(0, 1) assert I.from_string(i2, **params) == I.openclosed(0, 1) assert I.from_string(i3, **params) == I.closedopen(0, 1) assert I.from_string(i4, **params) == I.open(0, 1) assert I.from_string('<!!>', **params) == I.empty() assert I.from_string('<"1">', **params) == I.singleton(1) assert I.from_string('<!-oo-"1">', **params) == I.openclosed(-I.inf, 1) assert I.from_string('<"1"-+oo!>', **params) == I.closedopen(1, I.inf) assert I.from_string('<"0"-"1"> or <"2"-"3">', **params) == I.closed(0, 1) | I.closed(2, 3)
def test_overlaps_permissive(): assert I.closed(0, 1).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.open(0, 1), permissive=True) assert I.open(0, 1).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.openclosed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.closedopen(0, 1), permissive=True) assert I.closed(1, 2).overlaps(I.closed(2, 3), permissive=True) assert I.closed(1, 2).overlaps(I.closedopen(2, 3), permissive=True) assert I.openclosed(1, 2).overlaps(I.closed(2, 3), permissive=True) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3), permissive=True) assert not I.closed(0, 1).overlaps(I.closed(3, 4), permissive=True) assert not I.closed(3, 4).overlaps(I.closed(0, 1), permissive=True) assert I.closed(0, 1).overlaps(I.open(1, 2), permissive=True) assert I.closed(0, 1).overlaps(I.openclosed(1, 2), permissive=True) assert I.closedopen(0, 1).overlaps(I.closed(1, 2), permissive=True) assert I.closedopen(0, 1).overlaps(I.closedopen(1, 2), permissive=True) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2), permissive=True) assert not I.closedopen(0, 1).overlaps(I.open(1, 2), permissive=True) assert not I.open(0, 1).overlaps(I.open(1, 2), permissive=True) assert not I.empty().overlaps(I.open(-I.inf, I.inf), permissive=True) assert not I.open(-I.inf, I.inf).overlaps(I.empty(), permissive=True)
def test_to_string_customized(): i1, i2, i3, i4 = I.closed(0, 1), I.openclosed(0, 1), I.closedopen(0, 1), I.open(0, 1) params = { 'disj': ' or ', 'sep': '-', 'left_open': '<!', 'left_closed': '<', 'right_open': '!>', 'right_closed': '>', 'conv': lambda s: '"{}"'.format(s), 'pinf': '+oo', 'ninf': '-oo', } assert I.to_string(i1, **params) == '<"0"-"1">' assert I.to_string(i2, **params) == '<!"0"-"1">' assert I.to_string(i3, **params) == '<"0"-"1"!>' assert I.to_string(i4, **params) == '<!"0"-"1"!>' assert I.to_string(I.empty(), **params) == '<!!>' assert I.to_string(I.singleton(1), **params) == '<"1">' assert I.to_string(I.openclosed(-I.inf, 1), **params) == '<!-oo-"1">' assert I.to_string(I.closedopen(1, I.inf), **params) == '<"1"-+oo!>' assert I.to_string(I.closed(0, 1) | I.closed(2, 3), **params) == '<"0"-"1"> or <"2"-"3">'
def test_difference(): assert I.closed(1, 4) - I.closed(1, 3) == I.openclosed(3, 4) assert I.closed(1, 4) - I.closed(1, 3).to_atomic() == I.openclosed(3, 4) assert I.closed(1, 4).to_atomic() - I.closed( 1, 3).to_atomic() == I.openclosed(3, 4) assert (I.closed(1, 4) - I.closed(1, 4)).is_empty() assert I.closed(0, 1) - I.closed(2, 3) == I.closed(0, 1) assert I.closed(0, 4) - I.closed(2, 3) == I.closedopen( 0, 2) | I.openclosed(3, 4)
def test_to_data(): assert I.to_data(I.closedopen(2, 3)) == [(I.CLOSED, 2, 3, I.OPEN)] assert I.to_data(I.openclosed(2, I.inf)) == [(I.OPEN, 2, float('inf'), I.OPEN)] assert I.to_data(I.closed(-I.inf, 2)) == [(I.OPEN, float('-inf'), 2, I.CLOSED)] assert I.to_data(I.empty()) == [(I.OPEN, float('inf'), float('-inf'), I.OPEN)] i = I.openclosed(-I.inf, 4) | I.closedopen(6, I.inf) assert I.to_data(i) == [(I.OPEN, float('-inf'), 4, I.CLOSED), (I.CLOSED, 6, float('inf'), I.OPEN)] assert I.to_data(i, conv=str, pinf='highest', ninf='lowest') == [(I.OPEN, 'lowest', '4', I.CLOSED), (I.CLOSED, '6', 'highest', I.OPEN)]
def test_complement(): assert ~I.closed(1, 2) == I.open(-I.inf, 1) | I.open(2, I.inf) assert ~I.open(1, 2) == I.openclosed(-I.inf, 1) | I.closedopen(2, I.inf) intervals = [I.closed(0, 1), I.open(0, 1), I.openclosed(0, 1), I.closedopen(0, 1)] for interval in intervals: assert ~(~interval) == interval assert ~I.open(1, 1) == I.open(-I.inf, I.inf) assert (~I.closed(-I.inf, I.inf)).is_empty() assert ~I.empty() == I.open(-I.inf, I.inf)
def test_atomic_bounds(): i = I.openclosed(1, 2).to_atomic() assert i.left == I.OPEN assert i.right == I.CLOSED assert i.lower == 1 assert i.upper == 2 i = I.openclosed(10, -10).to_atomic() assert i.left == I.OPEN assert i.right == I.OPEN assert i.lower == I.inf assert i.upper == -I.inf
def test_from_data(): assert I.from_data([(I.CLOSED, 2, 3, I.OPEN)]) == I.closedopen(2, 3) assert I.from_data([(I.OPEN, 2, float('inf'), I.OPEN)]) == I.openclosed(2, I.inf) assert I.from_data([(I.OPEN, float('-inf'), 2, I.CLOSED)]) == I.closed(-I.inf, 2) assert I.from_data([]) == I.empty() d = [(I.OPEN, float('-inf'), 4, I.CLOSED), (I.CLOSED, 6, float('inf'), I.OPEN)] assert I.from_data(d) == I.openclosed(-I.inf, 4) | I.closedopen(6, I.inf) d = [(I.OPEN, 'lowest', '4', I.CLOSED), (I.CLOSED, '6', 'highest', I.OPEN)] assert I.from_data(d, conv=int, pinf='highest', ninf='lowest') == I.openclosed(-I.inf, 4) | I.closedopen(6, I.inf)
def test_iterate(): # Default parameters assert list(I.iterate(I.closed(0, 2), incr=1)) == [0, 1, 2] assert list(I.iterate(I.closedopen(0, 2), incr=1)) == [0, 1] assert list(I.iterate(I.openclosed(0, 2), incr=1)) == [1, 2] assert list(I.iterate(I.open(0, 2), incr=1)) == [1] assert list(I.iterate(I.open(0, 2.5), incr=1)) == [1, 2] # Empty intervals or iterations assert list(I.iterate(I.empty(), incr=1)) == [] assert list(I.iterate(I.open(0, 1), incr=1)) == [] # Infinities with pytest.raises(ValueError): list(I.iterate(I.openclosed(-I.inf, 2), incr=1)) gen = I.iterate(I.closedopen(0, I.inf), incr=1) assert next(gen) == 0 assert next(gen) == 1 assert next(gen) == 2 # and so on # Unions assert list(I.iterate(I.closed(0, 1) | I.closed(5, 6), incr=1)) == [0, 1, 5, 6] assert list(I.iterate(I.closed(0, 1) | I.closed(2.5, 4), incr=1)) == [0, 1, 2.5, 3.5] assert list(I.iterate(I.open(0, 1) | I.open(1, 2), incr=1)) == [] assert list(I.iterate(I.open(0.5, 1) | I.open(1, 3), incr=1)) == [2] # Step assert list(I.iterate(I.closed(0, 6), incr=2)) == [0, 2, 4, 6] assert list(I.iterate(I.closed(0, 6), incr=4)) == [0, 4] assert list(I.iterate(I.closed(0, 6), incr=lambda x: x + 2)) == [0, 2, 4, 6] # Base assert list(I.iterate(I.closed(0.4, 2), incr=1, base=lambda x: round(x))) == [1, 2] assert list(I.iterate(I.closed(0.6, 2), incr=1, base=lambda x: round(x))) == [1, 2] # Reversed assert list(I.iterate(I.closed(0, 1), incr=1, reverse=True)) == [1, 0] assert list(I.iterate(I.open(0, 3), incr=1, reverse=True)) == [2, 1] assert list(I.iterate(I.closed(0, 1), incr=0.5, reverse=True)) == [1, 0.5, 0] assert list(I.iterate(I.closed(0, 2), incr=1, base=lambda x: x-1, reverse=True)) == [1, 0] assert list(I.iterate(I.closed(0, 2) | I.closed(4, 5), incr=1, reverse=True)) == [5, 4, 2, 1, 0] with pytest.raises(ValueError): list(I.iterate(I.closedopen(0, I.inf), incr=1, reverse=True)) gen = I.iterate(I.openclosed(-I.inf, 2), incr=1, reverse=True) assert next(gen) == 2 assert next(gen) == 1 assert next(gen) == 0 # and so on
def test_to_string(): i1, i2, i3, i4 = I.closed(0, 1), I.openclosed(0, 1), I.closedopen(0, 1), I.open(0, 1) assert I.to_string(i1) == '[0,1]' assert I.to_string(i2) == '(0,1]' assert I.to_string(i3) == '[0,1)' assert I.to_string(i4) == '(0,1)' assert I.to_string(I.empty()) == '()' assert I.to_string(I.singleton(1)) == '[1]' assert I.to_string(I.openclosed(-I.inf, 1)) == '(-inf,1]' assert I.to_string(I.closedopen(1, I.inf)) == '[1,+inf)' assert I.to_string(I.closed(0, 1) | I.closed(2, 3)) == '[0,1] | [2,3]'
def test_emptiness(): assert I.openclosed(1, 1).is_empty() assert I.closedopen(1, 1).is_empty() assert I.open(1, 1).is_empty() assert not I.closed(1, 1).is_empty() assert I.Interval().is_empty() assert I.empty().is_empty()
def test_union(): assert I.closed(1, 2).to_atomic() | I.closed(1, 2).to_atomic() == I.closed(1, 2).to_atomic() assert I.closed(1, 4).to_atomic() | I.closed(2, 3).to_atomic() == I.closed(1, 4).to_atomic() assert I.closed(1, 2).to_atomic() | I.closed(2, 3).to_atomic() == I.closed(2, 3).to_atomic() | I.closed(1, 2).to_atomic() assert I.closed(1, 2).to_atomic() | I.closed(3, 4).to_atomic() == I.closed(1, 2) | I.closed(3, 4) assert I.closed(1, 2) | I.closed(1, 2) == I.closed(1, 2) assert I.closed(1, 4) | I.closed(2, 3) == I.closed(2, 3) | I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3) == I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3).to_atomic() == I.closed(1, 4) assert I.closed(1, 4) | I.closed(2, 3).to_atomic() == I.closed(2, 3).to_atomic() | I.closed(1, 4) assert I.closed(1, 2) | I.open(2, 3) == I.closedopen(1, 3) assert I.closed(1, 3) | I.closed(2, 4) == I.closed(1, 4) assert I.closed(1, 2) | I.closed(2, 3) == I.closed(2, 3) | I.closed(1, 2) assert I.closedopen(1, 2) | I.closed(2, 3) == I.closed(1, 3) assert I.open(1, 2) | I.closed(2, 4) == I.openclosed(1, 4) assert I.closed(1, 2) | I.closed(3, 4) != I.closed(1, 4) assert (I.closed(1, 2) | I.closed(3, 4) | I.closed(2, 3)).is_atomic() assert I.closed(1, 2) | I.closed(3, 4) | I.closed(2, 3) == I.closed(1, 4) assert I.closed(1, 2) | I.closed(0, 4) == I.closed(0, 4) assert (I.closed(0, 1) | I.closed(2, 3) | I.closed(1, 2)).is_atomic() assert I.closed(0, 1) | I.closed(2, 3) | I.closed(1, 2) == I.closed(0, 3) assert I.closed(0, 1) | I.empty() == I.closed(0, 1)
def test_intersection(): assert I.closed(0, 1) & I.closed(0, 1) == I.closed(0, 1) assert I.closed(0, 1) & I.closed(0, 1).to_atomic() == I.closed(0, 1) assert I.closed(0, 1) & I.open(0, 1) == I.open(0, 1) assert I.openclosed(0, 1) & I.closedopen(0, 1) == I.open(0, 1) assert (I.closed(0, 1) & I.closed(2, 3)).is_empty() assert I.closed(0, 1) & I.empty() == I.empty()
def test_from_string(): i1, i2, i3, i4 = '[0,1]', '(0,1]', '[0,1)', '(0,1)' assert I.from_string(i1, int) == I.closed(0, 1) assert I.from_string(i2, int) == I.openclosed(0, 1) assert I.from_string(i3, int) == I.closedopen(0, 1) assert I.from_string(i4, int) == I.open(0, 1) assert I.from_string('()', int) == I.empty() assert I.from_string('[1]', int) == I.singleton(1) assert I.from_string('(-inf,1]', int) == I.openclosed(-I.inf, 1) assert I.from_string('[1,+inf)', int) == I.closedopen(1, I.inf) assert I.from_string('[0,1] | [2,3]', int) == I.closed(0, 1) | I.closed(2, 3) with pytest.raises(Exception): I.from_string('[1,2]', None)
def build_interval_draft4(self): if self.exclusiveMinimum and self.exclusiveMaximum: self.interval = I.open(self.minimum, self.maximum) elif self.exclusiveMinimum: self.interval = I.openclosed(self.minimum, self.maximum) elif self.exclusiveMaximum: self.interval = I.closedopen(self.minimum, self.maximum) else: self.interval = I.closed(self.minimum, self.maximum)
def test_bounds(): i = I.openclosed(1, 2) assert i.left == I.OPEN assert i.right == I.CLOSED assert i.lower == 1 assert i.upper == 2 i = I.openclosed(10, -10) assert i.left == I.OPEN assert i.right == I.OPEN assert i.lower == I.inf assert i.upper == -I.inf i = I.open(0, 1) | I.closed(3, 4) assert i.left == I.OPEN assert i.right == I.CLOSED assert i.lower == 0 assert i.upper == 4
def test_creation(): assert I.closed(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.CLOSED) assert I.open(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.OPEN) assert I.openclosed(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.CLOSED) assert I.closedopen(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.OPEN) assert I.closed(-I.inf, I.inf) == I.open(-I.inf, I.inf) with pytest.raises(ValueError): I.closed(1, -1) assert I.singleton(2) == I.closed(2, 2) assert I.Interval() == I.open(0, 0) assert I.empty() == I.Interval()
def test_replace_atomic(): i = I.closed(0, 1).to_atomic() assert i.replace() == i assert i.replace(I.OPEN, 2, 3, I.OPEN) == I.open(2, 3) assert i.replace(upper=2, left=I.OPEN) == I.openclosed(0, 2) assert i.replace(lower=lambda v: 1 + v) == I.singleton(1) assert i.replace(left=lambda v: not v, right=lambda v: not v) == I.open(0, 1) assert I.empty().to_atomic().replace(left=I.CLOSED, right=I.CLOSED) == I.empty() assert I.empty().to_atomic().replace(lower=1, upper=2) == I.open(1, 2) assert I.empty().to_atomic().replace(lower=lambda v: 1, upper=lambda v: 2) == I.empty() assert I.empty().to_atomic().replace(lower=lambda v: 1, upper=lambda v: 2, ignore_inf=False) == I.open(1, 2)
def test_interval_to_atomic(): intervals = [I.closed(0, 1), I.open(0, 1), I.openclosed(0, 1), I.closedopen(0, 1)] for interval in intervals: assert interval == I.Interval(interval.to_atomic()) assert interval == interval.to_atomic() assert I.closed(0, 1) | I.closed(2, 3) != I.closed(0, 3) assert (I.closed(0, 1) | I.closed(2, 3)).to_atomic() == I.closed(0, 3) assert I.closed(0, 1).to_atomic() == I.closed(0, 1).enclosure() assert (I.closed(0, 1) | I.closed(2, 3)).enclosure() == I.closed(0, 3) assert I.empty().to_atomic() == I.AtomicInterval(False, I.inf, -I.inf, False)
def test_hard_atomic_constraints(): i = I.closed(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.open(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and not allows_patch(i, soft=False) i = I.closed(Version('1.0.0'), Version('1.1.0')).to_atomic() assert not allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.open(Version('1.0.0'), Version('1.1.0')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.closed(Version('1.0.0'), Version('1.0.0')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and not allows_patch(i, soft=False) i = I.closed(Version('1.0.0'), Version('2.0.0')).to_atomic() assert allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.open(Version('1.0.0'), Version('2.0.0')).to_atomic() assert not allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.openclosed(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and not allows_patch(i, soft=False) i = I.closedopen(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and not allows_patch(i, soft=False) i = I.open(Version('1.0.0'), I.inf).to_atomic() assert allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.closed(Version.FIRST, Version('1.0.0')).to_atomic() assert allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.open(Version.FIRST, Version('1.0.0')).to_atomic() assert not allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.closed(Version.FIRST, Version.FIRST).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and not allows_patch(i, soft=False) i = I.closed(Version.FIRST, Version('0.0.2')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.open(Version.FIRST, Version('0.0.2')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and not allows_patch(i, soft=False) i = I.closed(Version.FIRST, Version('0.1.0')).to_atomic() assert not allows_major(i, soft=False) and allows_minor(i, soft=False) and allows_patch(i, soft=False) i = I.open(Version.FIRST, Version('0.1.0')).to_atomic() assert not allows_major(i, soft=False) and not allows_minor(i, soft=False) and allows_patch(i, soft=False)
def closest2bed(lines): clsExonDict = defaultdict(list) for li in lines: cell = li.split('\t') qry_id = cell[3] pro_id = cell[9] strand_q = cell[5] strand = cell[11] dist = int(cell[12]) if dist == 0: start_lnc, end_lnc = int(cell[1]), int(cell[2]) start_pro, end_pro = int(cell[7]), int(cell[8]) region_1 = intervals.openclosed(start_lnc, end_lnc) region_2 = intervals.openclosed(start_pro, end_pro) if not region_1 & region_2: dist = 1 if qry_id in clsExonDict: if dist < clsExonDict[qry_id][1]: clsExonDict[qry_id] = [pro_id, dist, strand_q, strand] else: clsExonDict[qry_id] = [pro_id, dist, strand_q, strand] return clsExonDict
def comparator_interval(op, version): if op == '=': return I.singleton(version) if op == '<': return I.closedopen(Version.FIRST, version) if op == '<=': return I.closed(Version.FIRST, version) if op == '>': return I.open(version, I.inf) if op == '>=': return I.closedopen(version, I.inf) if op == '!=': return I.closedopen(Version.FIRST, version) | I.openclosed( version, I.inf)
def test_overlaps(): # Overlaps should reject non supported types with pytest.raises(TypeError): I.closed(0, 1).to_atomic().overlaps(1) with pytest.raises(TypeError): I.closed(0, 1).overlaps(1) assert I.closed(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.open(0, 1)) assert I.open(0, 1).overlaps(I.closed(0, 1)) assert I.closed(0, 1).overlaps(I.openclosed(0, 1)) assert I.closed(0, 1).overlaps(I.closedopen(0, 1)) assert I.closed(1, 2).overlaps(I.closed(2, 3)) assert I.closed(1, 2).overlaps(I.closedopen(2, 3)) assert I.openclosed(1, 2).overlaps(I.closed(2, 3)) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3)) assert not I.closed(0, 1).overlaps(I.closed(3, 4)) assert not I.closed(3, 4).overlaps(I.closed(0, 1)) assert not I.closed(0, 1).overlaps(I.open(1, 2)) assert not I.closed(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closedopen(1, 2)) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.open(1, 2)) assert not I.open(0, 1).overlaps(I.open(1, 2)) assert not I.empty().overlaps(I.open(-I.inf, I.inf)) assert not I.open(-I.inf, I.inf).overlaps(I.empty()) # https://github.com/AlexandreDecan/python-intervals/issues/13 assert not I.closed(0, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(0, 1).overlaps(I.closed(1, 2)) assert not I.closed(1, 1).overlaps(I.openclosed(1, 2)) assert not I.closedopen(1, 1).overlaps(I.closed(1, 2)) assert not I.openclosed(1, 2).overlaps(I.closed(0, 1)) assert not I.openclosed(1, 2).overlaps(I.closed(1, 1)) assert I.open(0, 2).overlaps(I.open(0, 1)) assert I.open(0, 1).overlaps(I.open(0, 2))
def test_soft_atomic_constraints(): i = I.closed(Version('1.0.0'), Version('2.0.0')).to_atomic() assert not allows_major(i) and allows_minor(i) and allows_patch(i) i = I.closedopen(Version('1.0.0'), Version('2.0.0')).to_atomic() assert not allows_major(i) and allows_minor(i) and allows_patch(i) i = I.openclosed(Version('1.0.0'), Version('2.0.0')).to_atomic() assert not allows_major(i) and allows_minor(i) and allows_patch(i) i = I.open(Version('1.0.0'), Version('2.0.0')).to_atomic() assert not allows_major(i) and allows_minor(i) and allows_patch(i) i = I.closed(Version('1.0.0'), Version('1.1.0')).to_atomic() assert not allows_major(i) and not allows_minor(i) and allows_patch(i) i = I.closedopen(Version('1.0.0'), Version('1.1.0')).to_atomic() assert not allows_major(i) and not allows_minor(i) and allows_patch(i) i = I.openclosed(Version('1.0.0'), Version('1.1.0')).to_atomic() assert not allows_major(i) and not allows_minor(i) and allows_patch(i) i = I.open(Version('1.0.0'), Version('1.1.0')).to_atomic() assert not allows_major(i) and not allows_minor(i) and allows_patch(i) i = I.closed(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i) and not allows_minor(i) and not allows_patch(i) i = I.closedopen(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i) and not allows_minor(i) and not allows_patch(i) i = I.openclosed(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i) and not allows_minor(i) and not allows_patch(i) i = I.open(Version('1.0.0'), Version('1.0.1')).to_atomic() assert not allows_major(i) and not allows_minor(i) and not allows_patch(i)
def test_intervaldict_combine(): add = lambda x, y: x + y assert I.IntervalDict().combine(I.IntervalDict(), add) == I.IntervalDict() d = I.IntervalDict([(I.closed(0, 3), 0)]) assert I.IntervalDict().combine(d, add) == d assert d.combine(I.IntervalDict(), add) == d d1 = I.IntervalDict([(I.closed(1, 3) | I.closed(5, 7), 1)]) d2 = I.IntervalDict([(I.closed(2, 4) | I.closed(6, 8), 2)]) assert d1.combine(d2, add) == d2.combine(d1, add) assert d1.combine(d2, add) == I.IntervalDict([ (I.closedopen(1, 2) | I.closedopen(5, 6), 1), (I.closed(2, 3) | I.closed(6, 7), 3), (I.openclosed(3, 4) | I.openclosed(7, 8), 2), ]) d1 = I.IntervalDict({ I.closed(0, 1): 2, I.closed(3, 4): 2 }) d2 = I.IntervalDict({ I.closed(1, 3): 3, I.closed(4, 5): 1 }) assert d1.combine(d2, add) == d2.combine(d1, add) assert d1.combine(d2, add) == I.IntervalDict({ I.closedopen(0, 1): 2, I.singleton(1): 5, I.open(1, 3): 3, I.singleton(3): 5, I.open(3, 4): 2, I.singleton(4): 3, I.openclosed(4, 5): 1, })
def test_to_interval_to_atomic(): intervals = [ I.closed(0, 1), I.open(0, 1), I.openclosed(0, 1), I.closedopen(0, 1) ] for interval in intervals: assert interval == I.Interval(interval.to_atomic()) assert interval == interval.to_atomic() assert I.closed(0, 1) | I.closed(2, 3) != I.closed(0, 3) assert (I.closed(0, 1) | I.closed(2, 3)).to_atomic() == I.closed(0, 3) assert I.closed(0, 1).to_atomic() == I.closed(0, 1).enclosure() assert (I.closed(0, 1) | I.closed(2, 3)).enclosure() == I.closed(0, 3)
def test_overlaps_adjacent(): # Check warnings for "permissive" with pytest.warns(DeprecationWarning, match='permissive'): I.closed(0, 1).to_atomic().overlaps(I.open(3, 4).to_atomic(), permissive=True) with pytest.warns(DeprecationWarning, match='permissive'): I.closed(0, 1).overlaps(I.open(3, 4), permissive=True) assert I.closed(0, 1).overlaps(I.closed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.open(0, 1), adjacent=True) assert I.open(0, 1).overlaps(I.closed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.openclosed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.closedopen(0, 1), adjacent=True) assert I.closed(1, 2).overlaps(I.closed(2, 3), adjacent=True) assert I.closed(1, 2).overlaps(I.closedopen(2, 3), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closed(2, 3), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closedopen(2, 3), adjacent=True) assert not I.closed(0, 1).overlaps(I.closed(3, 4), adjacent=True) assert not I.closed(3, 4).overlaps(I.closed(0, 1), adjacent=True) assert I.closed(0, 1).overlaps(I.open(1, 2), adjacent=True) assert I.closed(0, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert I.closedopen(0, 1).overlaps(I.closed(1, 2), adjacent=True) assert I.closedopen(0, 1).overlaps(I.closedopen(1, 2), adjacent=True) assert not I.closedopen(0, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert not I.closedopen(0, 1).overlaps(I.open(1, 2), adjacent=True) assert not I.open(0, 1).overlaps(I.open(1, 2), adjacent=True) assert not I.empty().overlaps(I.open(-I.inf, I.inf), adjacent=True) assert not I.open(-I.inf, I.inf).overlaps(I.empty(), adjacent=True) # https://github.com/AlexandreDecan/python-intervals/issues/13 assert I.closed(0, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert I.closedopen(0, 1).overlaps(I.closed(1, 2), adjacent=True) assert I.closed(1, 1).overlaps(I.openclosed(1, 2), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closed(0, 1), adjacent=True) assert I.openclosed(1, 2).overlaps(I.closed(1, 1), adjacent=True)