def quantize(begin, end, subdiv, breakouts): lapse = Lapse(begin, timedelta(hours=1) / subdiv) results = [] iterator = iter(breakouts) repeat = 0 try: breakout = next(iterator) while lapse.precedes(end): if lapse.precedes(breakout.start_at): repeat = 0 results.append((lapse.start_at, Nothing(), repeat)) elif breakout.lapse.contains(lapse): results.append((lapse.start_at, maybe(breakout), repeat)) repeat += 1 else: repeat = 0 breakout = next(iterator) continue lapse.increment() except StopIteration: while lapse.precedes(end): results.append((lapse.start_at, Nothing(), repeat)) lapse.increment() finally: return results
def test_comparisons(self): self.assertReallyEqual(Something(1), 1) self.assertReallyEqual(1, Something(1)) self.assertReallyEqual(Something(1), Something(1)) self.assertReallyEqual(Nothing(), Nothing()) self.assertReallyNotEqual(Something(1), Something(2)) self.assertReallyNotEqual(Something(1), Nothing()) self.assertReallyNotEqual(Nothing(), Something(1))
def test_nothing_cmp(self): if PY2: self.assertEqual(0, cmp(Nothing(), Nothing())) self.assertEqual(1, cmp(1, Nothing())) self.assertEqual(1, cmp(Something(5), Nothing())) self.assertEqual(1, cmp(5, Nothing())) self.assertEqual(-1, cmp(Nothing(), Something(5))) self.assertEqual(-1, cmp(Nothing(), 5))
def test_something_cmp(self): l = [Something(1), 2, Nothing()] sortedl = sorted(l) self.assertTrue(isinstance(sortedl[0], Nothing)) self.assertTrue(isinstance(sortedl[1], Something)) self.assertTrue(isinstance(sortedl[2], int)) self.assertEqual(sortedl[0], None) self.assertEqual(sortedl[1], 1) self.assertEqual(sortedl[2], 2)
def test_something_cmp(self): if PY2: n = Nothing() s = maybe(5) s1 = maybe(7) self.assertEqual(1, cmp(s, n)) self.assertEqual(cmp(5, 5), cmp(s, s)) self.assertEqual(cmp(5, 7), cmp(s, s1)) self.assertEqual(cmp(7, 5), cmp(s1, s)) self.assertEqual(cmp(5, 5), cmp(s, 5)) self.assertEqual(cmp(5, 7), cmp(s, 7)) self.assertEqual(cmp(7, 5), cmp(7, s))
def test_nothing_neSomething(self): self.assertTrue(Nothing() != Something(2)) self.assertTrue(Something(1) != Nothing())
def test_nothing_notEqualToSomething(self): self.assertFalse(Nothing() == Something(2)) self.assertFalse(Something(1) == Nothing())
def test_nothing_equalToNothing(self): self.assertTrue(Nothing() == Nothing())
def test_nothing_getItem_returnsNothing(self): n = Nothing()['name'] self.assertTrue(isinstance(n, Nothing)) self.assertTrue(n.is_none()) self.assertFalse(n.is_some())
def test_nothing_setItem_doestNothing(self): Nothing()['name'] = 'value' # Will raise if __setitem__ wasnt defined
def test_nothing_unicode(self): if PY2: self.assertEqual(unicode(Nothing()), unicode(None))
def test_nothing_ltNone_isFalse(self): self.assertFalse(Nothing() < None)
def test_nothing_gtAnything_isFalse(self): self.assertFalse(Nothing() > Nothing()) self.assertFalse(Nothing() > Something(123)) self.assertFalse(Nothing() > None) self.assertFalse(Nothing() > "Value")
def test_something_cmp_greaterThanNothing(self): l = [Something(0), Nothing()] sortedl = sorted(l) self.assertTrue(isinstance(sortedl[0], Nothing)) self.assertTrue(isinstance(sortedl[1], Something))
def test_nothing_strings_returnNone(self): self.assertEqual(str(Nothing()), "Nothing")
def test_nothing_getItem_returnsNone(self): result = Nothing()[10] self.assertIsInstance(result, Nothing)
def test_nothing_length_isZero(self): self.assertEqual(len(Nothing()), 0)
def test_nothing_nonzero_isFalse(self): self.assertFalse(bool(Nothing()))
def test_nothing_neNothing(self): self.assertFalse(Nothing() != Nothing())
def test_nothing_ltSomething_isTrue(self): self.assertTrue(Nothing() < Something(1))
def test_nothing_geNotNoneOrNothing_isFalse(self): self.assertFalse(Nothing() >= Something(2)) self.assertFalse(Nothing() >= "some")
def test_nothing_ltNotNone_isFalse(self): self.assertTrue(Nothing() < "some")
def test_nothing_delItem_doestNothing(self): del Nothing()['name'] # Will raise if __delitem__ wasnt defined
def test_nothing_leAnything_isTrue(self): self.assertTrue(Nothing() <= Nothing()) self.assertTrue(Nothing() <= Something(123)) self.assertTrue(Nothing() <= None) self.assertTrue(Nothing() <= "Value")
def test_something_leNothing_isFalse(self): self.assertFalse(Something("value") <= Nothing())
def test_something_geNothing_isTrue(self): self.assertTrue(Something("value") >= Nothing())
def test_nothing_geNone_isTrue(self): self.assertTrue(Nothing() >= None)