示例#1
0
  def test_pipe_valve_non_iterable(self):
    data_1 = 4, 2, 8, -5
    data_1_min = min(data_1)
    data_2 = ()

    # test method that directly passes the data through
    Pipe.add_map_method(lambda val: val, 'pass_through')
    # min
    Pipe.add_method(min, is_valve=True, empty_error=ValueError)

    '''
    The Pipe has been preloaded and a valve has been added to the pipe.
    When the preloaded and a valve is added the reservoir should be drained
    by the pipe into the valve function and the result should be returned.
    '''
    self.assertEqual(
        Pipe(data_1).min(),
        data_1_min
      )
    self.assertEqual(
        Pipe(data_1).pass_through().min(),
        data_1_min
      )

    '''
    Pipe has not been preloaded and a Pipe should be returned.
    '''
    pipe_1 = Pipe().min()
    self.assertEqual(pipe_1(data_1), data_1_min)
    self.assertEqual(pipe_1(data_1), data_1_min)  # not a repeat

    pipe_2 = Pipe().pass_through().min()
    self.assertEqual(pipe_2(data_1), data_1_min)
    self.assertEqual(pipe_2(data_1), data_1_min)  # not a repeat

    pip_3 = Pipe().min().pass_through()
    self.assertEqual(next(pip_3(data_1)), data_1_min)

    pip_4 = Pipe().min().min()
    self.assertEqual(pipe_1(data_1), data_1_min)

    pipe_5 = pipe_1.pass_through()
    self.assertEqual(
        next(pipe_5(data_1)),
        next(pip_3(data_1))
      )

    pipe_6 = pipe_1.min()
    self.assertEqual(
        pipe_6(data_1),
        pip_4(data_1)
      )

    # empty preloaded iterator
    with self.assertRaises(ValueError):
      print(Pipe(data_2).min())
示例#2
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)
示例#3
0
  def test_add_map_method(self):
    data_1 = 1, 2, 7, 9
    data_2 = (1, 2), (3, 4)
    data_3 = dict(a=1, b=2), dict(a=3, b=4)

    Pipe.add_map_method(lambda a: a**2, 'square')

    self.assertEqual(
        tuple(Pipe(data_1).square()),
        tuple(d**2 for d in data_1)
      )

    # method call includes a value
    Pipe.add_map_method(min)
    self.assertEqual(
        tuple(Pipe(data_1).min(5, key=lambda a: 1 / a)),
        (5, 5, 7, 9)
      )

    self.assertEqual(
        tuple(Pipe(data_2).min()),
        (1, 3)
      )

    self.assertEqual(
        tuple(Pipe(data_2).min(key=lambda a: 1 / a)),
        (2, 4)
      )

    # single star
    Pipe.add_map_method(lambda a, b: a + b, 'add', star_wrap=True)

    self.assertEqual(
        tuple(Pipe(data_2).add()),
        (3, 7)
      )

    # double star
    Pipe.add_map_method(lambda a, b: a + b, 'add', double_star_wrap=True, no_over_write=False)

    self.assertEqual(
        tuple(Pipe(data_3).add()),
        (3, 7)
      )
示例#4
0
for word in first_letter(data):
    print(word)

print(all_letters(data))
'''
Adding Methods (It is SOOOO EASY!)
So, you thought that the above lambda functions were ugly and hard to read. We can
fix that with Pipe.add_method or Pipe.add_map_method. Those methods allow for simple
and complex methods to be added to Pipe.

Below is an example of how to add the above examples methods to Pipe and get the
same result.
'''

# this as the string class method upper
Pipe.add_map_method(str.upper)

# lambda functions can also be added
Pipe.add_map_method(lambda word: word[0].upper() + word[1:],
                    name='first_upper')

# lets try again and see how much better this looks
first_letter = Pipe().first_upper()

all_letters = first_letter.upper().tuple()
'''
Carry Values
Meaning, avoiding the bs of passing values through every function and having to work around
them in every method.

This allows you to carry the key/value in a key value pair, a dict, list, or object that you