def test_iteration(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) assert_list_equal(list(interval_set1), [ Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False) ])
def test_nonzero(): interval1 = Interval(5., 10., upper_closed=False) interval2 = Interval(5., 5., lower_closed=False, upper_closed=False) interval3 = Interval(interval_type=float) assert bool(interval1) assert not bool(interval2) assert bool(interval3)
def test_date_interval_set_construction(): interval_set = IntervalSet(Interval(date(2012, 1, 1), date(2012, 2, 5)), Interval(date(2014, 12, 3), date(2015, 3, 17))) assert_equal(len(interval_set.intervals), 2) assert_equal(interval_set.intervals[0], Interval(date(2012, 1, 1), date(2012, 2, 5))) assert_equal(interval_set.intervals[1], Interval(date(2014, 12, 3), date(2015, 3, 17))) assert_is(type(interval_set), DateIntervalSet)
def test_contains(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) assert_in(.3, interval_set1) assert_not_in(1., interval_set1) assert_not_in(-0.00001, interval_set1) assert_not_in(3.000001, interval_set1) assert_in(2., interval_set1) assert_in(2, interval_set1) # int automatically cast as double assert_raises(TypeError, lambda: 'x' in interval_set1)
def test_interval_type_argument(): interval = Interval(interval_type=1.) assert isinstance(interval, FloatInterval) interval = Interval(interval_type=float) assert isinstance(interval, FloatInterval) interval = Interval(interval_type=FloatInterval) assert isinstance(interval, FloatInterval) assert_raises( TypeError, lambda: Interval(lower_bound=date(2012, 1, 1), interval_type=FloatInterval))
def test_float_interval_contains(): interval = Interval(5., 10., upper_closed=False) assert_in(5., interval) assert_in(5, interval) assert_not_in(10., interval) assert_in(9.9999, interval) assert_not_in(4.99999, interval) assert_in(6.5, interval)
def test_union(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) interval_set2 = IntervalSet(Interval(.5, 1.5)) assert_equal( interval_set1.union(interval_set2).intervals, (Interval(0., 3.), )) interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False)) interval_set2 = IntervalSet(Interval(1., 3., lower_closed=False)) assert_equal( interval_set1.union(interval_set2).intervals, (Interval( 0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)))
def test_fusion(): interval1 = Interval(1., 2.) interval2 = Interval(2., 3., upper_closed=False) fused = interval1.fusion(interval2) assert_equal(fused.lower_bound, 1.) assert_equal(fused.lower_closed, True) assert_equal(fused.lower_bounded, True) assert_equal(fused.upper_bound, 3.) assert_equal(fused.upper_closed, False) assert_equal(fused.upper_bounded, True) interval1 = Interval(1., 2.) interval2 = Interval(unbounded, 3., upper_closed=False) fused = interval1.fusion(interval2) assert_equal(interval2, fused)
def test_intersection(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) interval_set2 = IntervalSet(Interval(.5, 1.5)) assert_equal( interval_set1.intersection(interval_set2).intervals, IntervalSet(Interval(.5, 1., upper_closed=False), Interval(1., 1.5, lower_closed=False)).intervals) interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False)) interval_set2 = IntervalSet(Interval(1., 3., lower_closed=False)) assert_equal(interval_set1.intersection(interval_set2).intervals, tuple())
def test_object_inerval_factory(): class HeavyFloat(object): def __init__(self, val): self.val = val def __lt__(self, other): return self.val < other.val def __eq__(self, other): return self.val == other.val one = HeavyFloat(1.) two = HeavyFloat(2.) interval = Interval(one, two) assert isinstance(interval, ObjectInterval)
def test_minus(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) interval_set2 = IntervalSet(Interval(.5, 1.5)) assert_equal( interval_set1.minus(interval_set2).intervals, (Interval(0., .5, upper_closed=False), Interval(1.5, 3., lower_closed=False))) assert_equal( interval_set2.minus(interval_set1).intervals, (Interval(1., 1.), ))
def test_object_interval_contains(): class HeavyFloat(object): def __init__(self, val): self.val = val def __lt__(self, other): return self.val < other.val def __eq__(self, other): return self.val == other.val one = HeavyFloat(1.) two = HeavyFloat(2.) interval = Interval(one, two) assert_in(HeavyFloat(1.5), interval) assert_not_in(HeavyFloat(2.5), interval)
def test_complement(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) assert_equal(interval_set1.complement().intervals, (Interval(unbounded, 0., upper_closed=False), Interval( 1., 1.), Interval(3., unbounded, lower_closed=False))) assert_equal( IntervalSet(interval_type=float).complement().intervals, IntervalSet(Interval(unbounded, unbounded, interval_type=float)).intervals) assert_equal(interval_set1.complement().complement(), interval_set1)
def test_operators(): interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) interval_set2 = IntervalSet(Interval(.5, 1.5)) assert_not_equal(interval_set1, interval_set2) assert_less_equal(interval_set1, interval_set1) assert_greater_equal(interval_set1, interval_set1) assert_equal(interval_set2, IntervalSet(Interval(.5, 1.5))) assert_less(interval_set1 & interval_set2, interval_set2) assert_less(interval_set1 & interval_set2, interval_set1) assert_less(interval_set1, interval_set1 | interval_set2) assert_less(interval_set2, interval_set1 | interval_set2) assert_equal(~~interval_set2, interval_set2) assert_equal((~interval_set2) & interval_set2, IntervalSet(interval_type=float)) assert_equal( (~interval_set2) | interval_set2, IntervalSet(Interval(unbounded, unbounded, interval_type=float))) assert_equal( interval_set1 - interval_set2, IntervalSet(Interval(0., .5, upper_closed=False), Interval(1.5, 3., lower_closed=False)))
def test_date_interval_factory_upper_unbounded(): interval = Interval(date(2012, 1, 1)) assert isinstance(interval, DateInterval) assert_equal(interval.lower_bound, date(2012, 1, 1)) assert_equal(interval.upper_bounded, False)
def test_interval_set_construction_single_interval(): interval_set = IntervalSet(Interval(0., 1.)) assert_equal(len(interval_set.intervals), 1) assert_equal(interval_set.intervals[0], Interval(0., 1.)) assert_is(type(interval_set), FloatIntervalSet)
def test_float_interval_factory(): interval = Interval(5., 10., upper_closed=False) assert isinstance(interval, FloatInterval) assert_equal(interval.lower_bound, 5.) assert_equal(interval.upper_bound, 10.) assert_equal(interval.upper_closed, False)
def test_richcmp(): interval1 = Interval(1., 2.) interval2 = Interval(2., 3.) assert_true(interval1.richcmp(interval2, 0)) assert_false(interval2.richcmp(interval1, 0)) assert_true(interval1.richcmp(interval2, 1)) assert_false(interval2.richcmp(interval1, 1)) assert_false(interval1.richcmp(interval2, 2)) assert_false(interval2.richcmp(interval1, 2)) assert_true(interval1.richcmp(interval2, 3)) assert_true(interval2.richcmp(interval1, 3)) assert_false(interval1.richcmp(interval2, 4)) assert_true(interval2.richcmp(interval1, 4)) assert_false(interval1.richcmp(interval2, 5)) assert_true(interval2.richcmp(interval1, 5))
def test_interval_set_construction_fusion(): interval_set = IntervalSet(Interval(0., 1.), Interval(1., 3.)) assert_equal(interval_set.intervals[0], Interval(0., 3.)) assert_equal(len(interval_set.intervals), 1) assert_is(type(interval_set), FloatIntervalSet) interval_set = IntervalSet(Interval(1., 3.), Interval(0., 1.)) assert_equal(interval_set.intervals[0], Interval(0., 3.)) assert_equal(len(interval_set.intervals), 1) assert_is(type(interval_set), FloatIntervalSet) interval_set = IntervalSet(Interval(0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)) assert_equal(interval_set.intervals[0], Interval(0., 1., upper_closed=False)) assert_equal(interval_set.intervals[1], Interval(1., 3., lower_closed=False)) assert_equal(len(interval_set.intervals), 2) assert_is(type(interval_set), FloatIntervalSet) interval_set = IntervalSet(Interval(1., 3., lower_closed=False), Interval(1., 1.), Interval(0., 1., upper_closed=False)) assert_equal(interval_set.intervals[0], Interval(0., 3.), str(interval_set.intervals[0])) assert_equal(len(interval_set.intervals), 1) assert_is(type(interval_set), FloatIntervalSet) interval_set = IntervalSet(Interval(0., 1.000001, upper_closed=False), Interval(1., 3., lower_closed=False)) assert_equal(interval_set.intervals[0], Interval(0., 3.)) assert_equal(len(interval_set.intervals), 1) assert_is(type(interval_set), FloatIntervalSet)
def test_int_interval_factory(): interval = Interval(5, 10, upper_closed=False) assert isinstance(interval, IntInterval) assert_equal(interval.lower_bound, 5) assert_equal(interval.upper_bound, 10) assert_equal(interval.upper_closed, False)
def test_upper_cmp(): interval1 = Interval(1., 2.) interval2 = Interval(1., 2.) assert_equal(interval1.upper_cmp(interval2), 0) interval1 = Interval(1., unbounded) interval2 = Interval(1., 2.) assert_equal(interval1.upper_cmp(interval2), 1) interval1 = Interval(1., 2.) interval2 = Interval(1., unbounded) assert_equal(interval1.upper_cmp(interval2), -1) interval1 = Interval(1., 2.) interval2 = Interval(1., 1.5) assert_equal(interval1.upper_cmp(interval2), 1) interval1 = Interval(1., 2.) interval2 = Interval(1., 4.) assert_equal(interval1.upper_cmp(interval2), -1) interval1 = Interval(1., 2.) interval2 = Interval(1., 2., upper_closed=False) assert_equal(interval1.upper_cmp(interval2), 1) interval1 = Interval(1., 2., upper_closed=False) interval2 = Interval(1., 2.) assert_equal(interval1.upper_cmp(interval2), -1)
def test_lower_cmp(): interval1 = Interval(1., 2.) interval2 = Interval(1., 4.) assert_equal(interval1.lower_cmp(interval2), 0) interval1 = Interval(unbounded, 2.) interval2 = Interval(1., 4.) assert_equal(interval1.lower_cmp(interval2), -1) interval1 = Interval(1., 2.) interval2 = Interval(unbounded, 4.) assert_equal(interval1.lower_cmp(interval2), 1) interval1 = Interval(1., 2.) interval2 = Interval(0., 4.) assert_equal(interval1.lower_cmp(interval2), 1) interval1 = Interval(0., 2.) interval2 = Interval(1., 4.) assert_equal(interval1.lower_cmp(interval2), -1) interval1 = Interval(1., 2.) interval2 = Interval(1., 4., lower_closed=False) assert_equal(interval1.lower_cmp(interval2), -1) interval1 = Interval(1., 2., lower_closed=False) interval2 = Interval(1., 4.) assert_equal(interval1.lower_cmp(interval2), 1)
def test_date_interval_factory(): interval = Interval(date(2012, 1, 1), date(2012, 4, 15)) assert isinstance(interval, DateInterval) assert_equal(interval.lower_bound, date(2012, 1, 1)) assert_equal(interval.upper_bound, date(2012, 4, 15))
def test_float_interval_set_construction(): interval_set = IntervalSet(Interval(0., 1.), Interval(2., 3.)) assert_equal(len(interval_set.intervals), 2) assert_equal(interval_set.intervals[0], Interval(0., 1.)) assert_equal(interval_set.intervals[1], Interval(2., 3.)) assert_is(type(interval_set), FloatIntervalSet)
def test_overlap_cmp(): interval1 = Interval(0., 1.) interval2 = Interval(-1., 0., upper_closed=False) assert_equal(interval2.overlap_cmp(interval1), -1) assert_equal(interval1.overlap_cmp(interval2), 1) interval3 = Interval(-1., 0.) assert_equal(interval3.overlap_cmp(interval1), 0) assert_equal(interval1.overlap_cmp(interval3), 0) assert_equal(interval3.overlap_cmp(interval2), 0) assert_equal(interval2.overlap_cmp(interval3), 0) interval4 = Interval(.5, .6) assert_equal(interval4.overlap_cmp(interval1), 0) assert_equal(interval1.overlap_cmp(interval4), 0) interval5 = Interval(5.5, 22.) assert_equal(interval5.overlap_cmp(interval1), 1) assert_equal(interval1.overlap_cmp(interval5), -1)
def test_default_type(): interval_set = IntervalSet(Interval(unbounded, unbounded)) assert_is(type(interval_set), ObjectIntervalSet) interval_set = IntervalSet() assert_is(type(interval_set), ObjectIntervalSet)
def test_empty(): interval = Interval(1., 1.1) assert_false(interval.empty()) interval = Interval(1., 1.1, lower_closed=False) assert_false(interval.empty()) interval = Interval(1., 1.1, lower_closed=False, upper_closed=False) assert_false(interval.empty()) interval = Interval(1., 1.) assert_false(interval.empty()) interval = Interval(1., 1., lower_closed=False) assert_true(interval.empty()) interval = Interval(1., 1., upper_closed=False) assert_true(interval.empty()) interval = Interval(1., 1., lower_closed=False, upper_closed=False) assert_true(interval.empty()) interval = Interval(1.0000001, 1.) assert_true(interval.empty()) # There are no integers between 1 and 2 interval = Interval(1, 2, lower_closed=False, upper_closed=False) assert_true(interval.empty()) interval = Interval(1, 2, lower_closed=False, upper_closed=True) assert_false(interval.empty()) interval = Interval(1, 2, lower_closed=True, upper_closed=False) assert_false(interval.empty()) # There are no days between December 31 and January 1 of consecutive years interval = Interval(date(2011, 12, 31), date(2012, 1, 1), lower_closed=False, upper_closed=False) assert_true(interval.empty()) interval = Interval(date(2011, 12, 31), date(2012, 1, 1), lower_closed=False, upper_closed=True) assert_false(interval.empty()) interval = Interval(date(2011, 12, 31), date(2012, 1, 1), lower_closed=True, upper_closed=False) assert_false(interval.empty())
from cyinterval.cyinterval import Interval, IntervalSet from collections import defaultdict positive = IntervalSet(Interval(lower_bound = 0, lower_closed=False)) nonnegative = IntervalSet(Interval(lower_bound = 0, lower_closed=True)) negative = IntervalSet(Interval(upper_bound = 0, upper_closed=False)) nonpositive = IntervalSet(Interval(upper_bound = 0, upper_closed=True)) def all_or_none(collection, attribute, ignore={}): if not collection: return None first = True result = None for item in collection: try: value = getattr(item,attribute) except AttributeError: continue if value in ignore: continue if first: result = value first = False elif value != result: return None return result def all_or_raise(collection, attribute, ignore={}): if not collection: return None first = True