Exemplo n.º 1
0
    def test_zero_items_returns_initial_empty_collection(self):
        collection = CollectingSink()

        transduce(Transducer,
                  sending(),
                  empty_iter(),
                  init=collection())

        self.assertEqual(len(collection), 0)
Exemplo n.º 2
0
    def test_two_items_causes_completion(self):
        singular_sink = SingularSink()

        transduce(Transducer,
                  sending(),
                  [23, 78],
                  init=singular_sink())

        self.assertTrue(singular_sink.has_value)
Exemplo n.º 3
0
    def test_two_items_returns_two_element_list(self):
        collection = CollectingSink()

        transduce(Transducer,
                  sending(),
                  [23, 78],
                  init=collection())

        self.assertEqual(list(collection), [23, 78])
Exemplo n.º 4
0
 def test_mutable_inits(self):
     """Tests that the same mutable init object isn't shared across invocations."""
     result = transduce(transducer=mapping(lambda x: x),
                        reducer=appending(),
                        iterable=range(3))
     self.assertListEqual(result, [0, 1, 2])
     result = transduce(transducer=mapping(lambda x: x),
                        reducer=appending(),
                        iterable=range(3))
     self.assertListEqual(result, [0, 1, 2])
Exemplo n.º 5
0
 def test_ordering_reverse_with_key(self):
     result = transduce(transducer=ordering(key=lambda x: len(x),
                                            reverse=True),
                        reducer=appending(),
                        iterable="The quick brown fox jumped".split())
     self.assertSequenceEqual(result,
                              ['jumped', 'quick', 'brown', 'The', 'fox'])
Exemplo n.º 6
0
 def test_ordering_preserves_mutable_sequence_type(self):
     result = transduce(transducer=ordering(),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8],
                        init=deque())
     self.assertIsInstance(result, deque)
     self.assertSequenceEqual(result, deque([2, 4, 6, 8, 10]))
Exemplo n.º 7
0
 def test_windowing_no_padding(self):
     result = transduce(transducer=windowing(3, window_type=list),
                        reducer=appending(),
                        iterable=[42, 12, 45, 9, 18, 3, 34, 13])
     self.assertListEqual(
         result, [[42], [42, 12], [42, 12, 45], [12, 45, 9], [45, 9, 18],
                  [9, 18, 3], [18, 3, 34], [3, 34, 13], [34, 13], [13]])
Exemplo n.º 8
0
 def test_ordering_preserves_mutable_sequence_type(self):
     result = transduce(transducer=ordering(),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8],
                        init=deque())
     self.assertIsInstance(result, deque)
     self.assertSequenceEqual(result, deque([2, 4, 6, 8, 10]))
Exemplo n.º 9
0
 def parse_group(lines):
     cmd, path_type, r, g, b = lines[0].split(';')
     color = float(r), float(g), float(b)
     strokes = transduce(
         compose(partitioning(lambda s: s.upper().startswith('MOVETO;')),
                 filtering(lambda lines: len(lines) > 1),
                 mapping(parse_path)), appending(), lines[1:])
     path = PltmePathGroup(strokes, path_type, color)
     return path
Exemplo n.º 10
0
 def test_chained_transducers(self):
     result = transduce(transducer=compose(mapping(lambda x: x * x),
                                           filtering(lambda x: x % 5 != 0),
                                           taking(6),
                                           dropping_while(lambda x: x < 15),
                                           distinct()),
                        reducer=appending(),
                        iterable=range(20))
     self.assertSequenceEqual(result, [16, 36, 49])
Exemplo n.º 11
0
 def test_chained_transducers(self):
     result = transduce(transducer=compose(
                       mapping(lambda x: x*x),
                       filtering(lambda x: x % 5 != 0),
                       taking(6),
                       dropping_while(lambda x: x < 15),
                       distinct()),
                   reducer=appending(),
                   iterable=range(20))
     self.assertSequenceEqual(result, [16, 36, 49])
Exemplo n.º 12
0
    def test_completing_with_summing_zero_items_returns_identity(self):

        def add(a, b):
            return a + b

        summing = completing(add, identity=0)

        result = transduce(Transducer,
                           summing,
                           [])
        self.assertEqual(result, 0)
Exemplo n.º 13
0
    def test_completing_with_summing_four_items(self):

        def add(a, b):
            return a + b

        summing = completing(add, identity=0)

        result = transduce(Transducer,
                           summing,
                           [4, 2, 1, 9])
        self.assertEqual(result, 16)
Exemplo n.º 14
0
    def test_completing_with_multiplying_zero_items_returns_identity(self):

        def multiply(a, b):
            return a * b

        multiplying = completing(multiply, identity=1)

        result = transduce(Transducer,
                           multiplying,
                           [])
        self.assertEqual(result, 1)
Exemplo n.º 15
0
    def test_completing_with_multiplying_four_items(self):

        def multiply(a, b):
            return a * b

        multiplying = completing(multiply, identity=1)

        result = transduce(Transducer,
                           multiplying,
                           [4, 2, 1, 9])
        self.assertEqual(result, 72)
Exemplo n.º 16
0
    def minmax_y(self):
        def r(accu, item):
            min_y = min(accu[0], item[0])
            max_y = max(accu[1], item[1])
            return min_y, max_y

        minmax_accu = transduce(transducer=pipe(
            mapping(lambda p: p.minmax_y()), reducing(r)),
                                reducer=expecting_single(),
                                iterable=self.paths)

        return minmax_accu
Exemplo n.º 17
0
 def test_windowing_padding(self):
     result = transduce(transducer=windowing(3, padding=0, window_type=list),
                        reducer=appending(),
                        iterable=[42, 12, 45, 9, 18, 3, 34, 13])
     self.assertListEqual(result,
                          [[0, 0, 42],
                           [0, 42, 12],
                           [42, 12, 45],
                           [12, 45, 9],
                           [45, 9, 18],
                           [9, 18, 3],
                           [18, 3, 34],
                           [3, 34, 13],
                           [34, 13, 0],
                           [13, 0, 0]])
Exemplo n.º 18
0
def parse_pltme(source: str):
    def parse_path(lines):
        def coords(s):
            cmd, x, y = s.split(';')
            return float(x), float(y)

        coords = [coords(l) for l in lines]
        return PltmePath(coords)

    def parse_group(lines):
        cmd, path_type, r, g, b = lines[0].split(';')
        color = float(r), float(g), float(b)
        strokes = transduce(
            compose(partitioning(lambda s: s.upper().startswith('MOVETO;')),
                    filtering(lambda lines: len(lines) > 1),
                    mapping(parse_path)), appending(), lines[1:])
        path = PltmePathGroup(strokes, path_type, color)
        return path

    return transduce(
        compose(mapping(lambda s: s.strip()),
                partitioning(lambda s: s.upper().startswith('STARTPATH;')),
                mapping(parse_group)), appending(), source.splitlines())
Exemplo n.º 19
0
 def test_reducing_with_init(self):
     result = transduce(transducer=reducing(operator.add, 10),
                        reducer=expecting_single(),
                        iterable=range(10))
     self.assertEqual(result, 55)
Exemplo n.º 20
0
 def test_scanning(self):
     result = transduce(transducer=scanning(operator.add),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 3, 6, 10])
Exemplo n.º 21
0
 def test_taking_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=taking(-3),
                   reducer=appending(),
                   iterable=[2, 4, 5, 8, 10])
Exemplo n.º 22
0
 def test_exactly_one_item(self):
     result = transduce(mapping(lambda x: x*x),
                        expecting_single(),
                        [42])
     self.assertEqual(result, 1764)
Exemplo n.º 23
0
 def test_two_items_returns_two_element_list(self):
     result = transduce(Transducer,
                        adding(),
                        [23, 78])
     self.assertEqual(result, {23, 78})
Exemplo n.º 24
0
 def test_conjoining_preserves_initial_sequence_type(self):
     result = transduce(Transducer,
                        conjoining(),
                        (23, 78),
                        init=[])
     self.assertEqual(result, [23, 78])
Exemplo n.º 25
0
 def test_two_items_returns_two_element_tuple(self):
     result = transduce(Transducer,
                        conjoining(),
                        [23, 78])
     self.assertEqual(result, (23, 78))
Exemplo n.º 26
0
 def test_taking_while(self):
     result = transduce(transducer=taking_while(lambda x: x < 6),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [2, 4, 5])
Exemplo n.º 27
0
 def test_dropping(self):
     result = transduce(transducer=dropping(3),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [8, 10])
Exemplo n.º 28
0
 def test_dropping_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=dropping(-3),
                   reducer=appending(),
                   iterable=[2, 4, 5, 8, 10])
Exemplo n.º 29
0
 def test_dropping(self):
     result = transduce(transducer=dropping(3),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [8, 10])
Exemplo n.º 30
0
 def test_taking_while(self):
     result = transduce(transducer=taking_while(lambda x: x < 6),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [2, 4, 5])
Exemplo n.º 31
0
 def test_appending_to_immutable_sequence_raises_attribute_error(self):
     with self.assertRaises(AttributeError):
         transduce(Transducer,
                   appending(),
                   (23, 78),
                   init=tuple())
Exemplo n.º 32
0
 def test_mapcatting(self):
     result = transduce(transducer=mapcatting(list),
                        reducer=appending(),
                        iterable=['new', 'found', 'land'])
     self.assertListEqual(result, list("newfoundland"))
Exemplo n.º 33
0
 def test_zero_items_returns_initial_empty_tuple(self):
     result = transduce(Transducer,
                        conjoining(),
                        empty_iter())
     self.assertEqual(result, tuple())
Exemplo n.º 34
0
 def test_sending_to_non_sink_raises_attribute_error(self):
     with self.assertRaises(AttributeError):
         transduce(Transducer,
                   sending(),
                   (23, 78),
                   init=set())
Exemplo n.º 35
0
 def test_conjoining_to_non_sequence_raises_type_error(self):
     with self.assertRaises(TypeError):
         transduce(Transducer,
                   conjoining(),
                   (23, 78),
                   init=set())
Exemplo n.º 36
0
 def test_enumerating_with_start(self):
     result = transduce(transducer=enumerating(start=3),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertListEqual(result, [(3, 2), (4, 4), (5, 6), (6, 8), (7, 10)])
Exemplo n.º 37
0
 def test_zero_items_returns_initial_empty_set(self):
     result = transduce(Transducer,
                        adding(),
                        empty_iter())
     self.assertEqual(result, set())
Exemplo n.º 38
0
 def test_enumerating(self):
     result = transduce(transducer=enumerating(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertListEqual(result, [(0, 2), (1, 4), (2, 6), (3, 8), (4, 10)])
Exemplo n.º 39
0
 def test_adding_to_non_set_raises_attribute_error(self):
     with self.assertRaises(AttributeError):
         transduce(Transducer,
                   adding(),
                   (23, 78),
                   init=tuple())
Exemplo n.º 40
0
 def test_scanning_with_init(self):
     result = transduce(transducer=scanning(operator.add, 3),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [3, 4, 6, 9, 13])
Exemplo n.º 41
0
 def test_too_many_items(self):
     with self.assertRaises(RuntimeError):
         transduce(mapping(lambda x: x*x),
                   expecting_single(),
                   [])
Exemplo n.º 42
0
 def test_mapcatting(self):
     result = transduce(transducer=mapcatting(list),
                        reducer=appending(),
                        iterable=['new', 'found', 'land'])
     self.assertListEqual(result, list("newfoundland"))