def splice(cls, S1, I1, S2, I2): intervals = [] ancestors = [] fwd = lambda I: P.closedopen(I.lower - I1.lower + I2.lower, I.upper - I1.upper + I2.upper) rev = lambda I: P.closedopen(I.lower - I2.lower + I1.lower, I.upper - I2.upper + I1.upper) for I, a in S1.items(): if I.overlaps(I1): d, o = I - I1, I & I1 if not d.empty: intervals.append(d) ancestors.append(a.transform(I, lambda I: I - I1)) # import ipdb; ipdb.set_trace() for II, aa in S2[P.Interval(*[fwd(atom) for atom in o])].items(): intervals.append(P.Interval(*[rev(atom) for atom in II])) ancestors.append(aa) else: intervals.append(I) ancestors.append(a) return cls(intervals, ancestors)
def test_slices(self): items = [P.closed(5, 6), P.closed(7, 9), P.singleton(10)] interval = P.Interval(*items) assert interval[:] == P.Interval(*items) assert interval[:2] == P.Interval(*items[:2]) assert interval[::-1] == P.Interval(*items[::-1]) assert interval[::2] == P.Interval(*items[::2])
def test_is_empty(self): assert P.openclosed(1, 1).empty assert P.closedopen(1, 1).empty assert P.open(1, 1).empty assert not P.closed(1, 1).empty assert P.Interval().empty assert P.empty().empty
def split_ticket_values(ticket, rules): valid_intervals = portion.Interval(*list(rules.values())) valid_values, invalid_values = [], [] for value in ticket: if value in valid_intervals: valid_values.append(value) else: invalid_values.append(value) return valid_values, invalid_values
def transform(interval, domain, func): fwd = lambda I: P.closedopen(I.lower - domain.lower + interval.lower, I. upper - domain.upper + interval.upper) rev = lambda I: P.closedopen(I.lower - interval.lower + domain.lower, I. upper - interval.upper + domain.upper) return P.Interval(*[ fwd(new_atom) for atom in interval for new_atom in func(rev(atom)) if not new_atom.empty ])
def get_rules(rules_as_string): rules = {} for rule in rules_as_string.split('\n'): name, values = rule.split(': ') rules[name] = portion.Interval() for value in values.split(' or '): interval = [int(v) for v in value.split('-')] interval.sort() rules[name] = rules[name].union(portion.closed(*interval)) return rules
def test_creation(self): assert P.Interval() == P.empty() assert P.Interval(P.closed(0, 1)) == P.closed(0, 1) assert P.Interval(P.closed(0, 1)) == P.closed(0, 1) assert P.Interval(P.closed(0, 1), P.closed(2, 3)) == P.closed(0, 1) | P.closed(2, 3) assert P.Interval(P.closed(0, 1) | P.closed(2, 3)) == P.closed(0, 1) | P.closed(2, 3) with pytest.raises(TypeError): P.Interval(1)
def test_creation_issue_19(self): # https://github.com/AlexandreDecan/python-intervals/issues/19 assert P.Interval(P.empty(), P.empty()) == P.empty()