コード例 #1
0
  def test_iter_next_call(self):
    # test iter and next
    sp1 = Reservoir()

    with self.assertRaises(StopIteration):
      next(sp1)

    data_1 = tuple(range(10))
    sp1(range(10))

    for s, d in zip_longest(sp1, data_1):
      self.assertEqual(s, d)


    # test call
    sp2 = Reservoir()

    with self.assertRaises(TypeError):
      next(sp2.iterator)

    # first fill
    data_1 = 1, 2, 3
    sp2(data_1)
    self.assertEqual(tuple(sp2), data_1)

    # second fill
    data_2 = 4, 5, 6
    sp2(data_2)
    self.assertEqual(tuple(sp2), data_2)

    # call when reservoir not yet empty
    sp2(data_1)
    with self.assertRaises(ValueError):
      sp2(data_1)
コード例 #2
0
  def test_init(self):
    Reservoir()

    data_1 = 1, 2, 3, 4
    sp1 = Reservoir(data_1)
    self.assertEqual(tuple(sp1.iterator), data_1)

    sp2 = Reservoir(data_1)
    self.assertIsNot(sp1, sp2)
コード例 #3
0
  def test_not_empty(self):
    data_1 = 1, 2
    data_2 = ()

    res_1 = Reservoir(data_1)
    self.assertTrue(res_1.not_empty())

    # tuple(zip(data_1, res_1))
    tuple(res_1)
    self.assertFalse(res_1.not_empty())

    res_1(data_1)
    self.assertTrue(res_1.not_empty())

    res_2 = Reservoir()
    self.assertFalse(res_2.not_empty())

    res_2(data_1)
    self.assertTrue(res_2.not_empty())
コード例 #4
0
  def test_drain_then_fill(self):
    data_1 = 1, 2, 4, 8
    data_2 = 5, 3, 6, 9

    res_1 = Reservoir(data_1)
    tuple(res_1)
    res_1(data_2)

    '''
    Should not trigger StopIteration in Reservoir.iterator. Iterator
    is now empty and should be allowed to be filled.
    '''
    for i in range(len(data_2)):
      next(res_1)
    res_1(data_1)
コード例 #5
0
  def test_iterable_object(self):
    '''
    tests functions that return an iterable object
    using sorted because it returns an iterable object
    '''
    test_function = sorted

    data_1 = 2, 1, 3
    data_2 = 5, 2, 6, 7, 2, 4, 6

    # test reuse with Reservoir
    resv_3 = Reservoir(data_1)
    valve_3 = Valve(func=test_function, iterator=resv_3, pass_args=(resv_3,))
    self.assertEqual(
        list(d for d, i in zip(valve_3, range(10))),
        sorted(data_1)
      )
    with self.assertRaises(StopIteration):
      next(valve_3)
    resv_3(data_2)
    self.assertTrue(next(valve_3), sorted(data_2))

    iter_4 = iter(data_1)
    valve_4 = Valve(func=test_function, iterator=iter_4, pass_args=(iter_4,))
    self.assertIs(next(valve_4), sorted(data_1)[0])
    for f, d in zip_longest(valve_4, sorted(data_1)[1:]):
      self.assertIs(f, d)

    # pass in a karg with pass_kargs
    iter_5 = iter(data_1)
    valve_5 = Valve(
        func = test_function,
        iterator = iter_5,
        pass_args = (iter_5,),
        pass_kargs = dict(reverse=True),
      )
    self.assertEqual(list(valve_5), sorted(data_1, reverse=True))
    with self.assertRaises(StopIteration):
      next(valve_5)
コード例 #6
0
  def test_non_iterable_object(self):
    '''
    tests functions that return a non iterable object
    using max because it returns a non iterable object
    '''
    test_function = max

    data_1 = 2, 1, 3
    max_1 = test_function(data_1)
    data_2 = 5, 2, 6, 7, 2, 4, 6
    max_2 = test_function(data_2)

    iter_1 = iter(data_1)
    valve_1 = Valve(
        func = test_function,
        iterator = iter_1,
        pass_args = (iter_1,),
        empty_error = ValueError,
      )
    result_1 = next(valve_1)
    self.assertIs(result_1, max_1)
    with self.assertRaises(StopIteration):
      next(valve_1)

    # test with reservoir
    resr_2 = Reservoir(data_1)
    valve_2 = Valve(
        func=test_function,
        iterator=resr_2,
        pass_args=(resr_2,),
        empty_error=ValueError
      )
    self.assertIs(next(valve_2), max_1)
    with self.assertRaises(StopIteration):
      next(valve_2)
    resr_2(data_2)
    self.assertIs(next(valve_2), max_2)
コード例 #7
0
    def test_init_next_iter(self):
        Pipe.add_map_method(lambda a: a**2, 'square')

        data_1 = (1, 2), (3, 4), (5, 6)
        drip_1 = Drip()
        res_1 = Reservoir(data_1)
        pipe_1 = Pipe(reservoir=drip_1).square()
        result_1 = tuple((a, b**2) for a, b in data_1)

        def carry_key(key_val):
            return key_val[0], key_val[1]

        def re_key(key, bypass_val):
            return key, bypass_val

        bpp = Bypass(
            bypass=pipe_1,
            iterable=res_1,
            drip_handle=drip_1,
            split=carry_key,
            merge=re_key,
        )

        self.assertEqual(tuple(bpp), result_1)
コード例 #8
0
 def new_handle(self, iterable=None):
   res_handle = ResHandle(self)
   self.reservoirs[res_handle] = Reservoir(iterable)
   return res_handle