def test_zero_items_returns_initial_empty_collection(self): collection = CollectingSink() transduce(Transducer, sending(), empty_iter(), init=collection()) self.assertEqual(len(collection), 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)
def test_two_items_returns_two_element_list(self): collection = CollectingSink() transduce(Transducer, sending(), [23, 78], init=collection()) self.assertEqual(list(collection), [23, 78])
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])
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'])
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]))
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]])
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]))
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
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])
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])
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)
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)
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)
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)
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
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]])
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())
def test_reducing_with_init(self): result = transduce(transducer=reducing(operator.add, 10), reducer=expecting_single(), iterable=range(10)) self.assertEqual(result, 55)
def test_scanning(self): result = transduce(transducer=scanning(operator.add), reducer=appending(), iterable=range(5)) self.assertListEqual(result, [0, 1, 3, 6, 10])
def test_taking_validation(self): with self.assertRaises(ValueError): transduce(transducer=taking(-3), reducer=appending(), iterable=[2, 4, 5, 8, 10])
def test_exactly_one_item(self): result = transduce(mapping(lambda x: x*x), expecting_single(), [42]) self.assertEqual(result, 1764)
def test_two_items_returns_two_element_list(self): result = transduce(Transducer, adding(), [23, 78]) self.assertEqual(result, {23, 78})
def test_conjoining_preserves_initial_sequence_type(self): result = transduce(Transducer, conjoining(), (23, 78), init=[]) self.assertEqual(result, [23, 78])
def test_two_items_returns_two_element_tuple(self): result = transduce(Transducer, conjoining(), [23, 78]) self.assertEqual(result, (23, 78))
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])
def test_dropping(self): result = transduce(transducer=dropping(3), reducer=appending(), iterable=[2, 4, 5, 8, 10]) self.assertListEqual(result, [8, 10])
def test_dropping_validation(self): with self.assertRaises(ValueError): transduce(transducer=dropping(-3), reducer=appending(), iterable=[2, 4, 5, 8, 10])
def test_dropping(self): result = transduce(transducer=dropping(3), reducer=appending(), iterable=[2, 4, 5, 8, 10]) self.assertListEqual(result, [8, 10])
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])
def test_appending_to_immutable_sequence_raises_attribute_error(self): with self.assertRaises(AttributeError): transduce(Transducer, appending(), (23, 78), init=tuple())
def test_mapcatting(self): result = transduce(transducer=mapcatting(list), reducer=appending(), iterable=['new', 'found', 'land']) self.assertListEqual(result, list("newfoundland"))
def test_zero_items_returns_initial_empty_tuple(self): result = transduce(Transducer, conjoining(), empty_iter()) self.assertEqual(result, tuple())
def test_sending_to_non_sink_raises_attribute_error(self): with self.assertRaises(AttributeError): transduce(Transducer, sending(), (23, 78), init=set())
def test_conjoining_to_non_sequence_raises_type_error(self): with self.assertRaises(TypeError): transduce(Transducer, conjoining(), (23, 78), init=set())
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)])
def test_zero_items_returns_initial_empty_set(self): result = transduce(Transducer, adding(), empty_iter()) self.assertEqual(result, set())
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)])
def test_adding_to_non_set_raises_attribute_error(self): with self.assertRaises(AttributeError): transduce(Transducer, adding(), (23, 78), init=tuple())
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])
def test_too_many_items(self): with self.assertRaises(RuntimeError): transduce(mapping(lambda x: x*x), expecting_single(), [])
def test_mapcatting(self): result = transduce(transducer=mapcatting(list), reducer=appending(), iterable=['new', 'found', 'land']) self.assertListEqual(result, list("newfoundland"))