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_chained_transducers(self): input = [0.0, 0.2, 0.8, 0.9, 1.1, 2.3, 2.6, 3.0, 4.1] output = CollectingSink() iterable_source(iterable=input, target=transduce( compose(pairwise(), mapping(lambda p: p[1] - p[0]), filtering(lambda d: d < 0.5), mapping(lambda _: "double-click")), target=output())) result = list(output) self.assertListEqual(result, ['double-click', 'double-click', 'double-click', 'double-click', 'double-click'])
def create3(): Observable.create(throw_error).transduce( compose( filtering(even), mapping(mul10)) ).subscribe() self.assertRaises(RxException, create3)
def test_chained_transducers(self): input = [0.0, 0.2, 0.8, 0.9, 1.1, 2.3, 2.6, 3.0, 4.1] output = CollectingSink() iterable_source(iterable=input, target=transduce(compose( pairwise(), mapping(lambda p: p[1] - p[0]), filtering(lambda d: d < 0.5), mapping(lambda _: "double-click")), target=output())) result = list(output) self.assertListEqual(result, [ 'double-click', 'double-click', 'double-click', 'double-click', 'double-click' ])
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_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()), iterable=range(20)) expected = [16, 36, 49] for r, e in zip(result, expected): self.assertEqual(r, e)
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 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_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(), ), iterable=range(20), ) expected = [16, 36, 49] for r, e in zip(result, expected): self.assertEqual(r, e)
def create(): return xs.transduce(compose(filtering(even), mapping(mul10)))
def create3(): Observable.create(throw_error).transduce( compose(filtering(even), mapping(mul10))).subscribe() self.assertRaises(RxException, create3)
def create2(): Observable.empty().transduce( compose(filtering(even), mapping(mul10))).subscribe_(noop, noop, throw_error)
def create(): Observable.throw_exception(RxException()).transduce( compose( filtering(even), mapping(mul10)) ).subscribe(noop, throw_error)
def test_too_many_items(self): with self.assertRaises(RuntimeError): transduce(mapping(lambda x: x*x), expecting_single(), [])
import transducer.lazy from transducer.transducers import filtering, mapping import cosmic_ray.commands import cosmic_ray.counting import cosmic_ray.modules import cosmic_ray.json_util import cosmic_ray.worker import cosmic_ray.testing import cosmic_ray.timing from cosmic_ray.work_db import use_db, WorkDB LOG = logging.getLogger() REMOVE_COMMENTS = mapping(lambda x: x.split('#')[0]) REMOVE_WHITESPACE = mapping(str.strip) NON_EMPTY = filtering(bool) CONFIG_FILE_PARSER = compose(REMOVE_COMMENTS, REMOVE_WHITESPACE, NON_EMPTY) def _load_file(config_file): """Read configuration from a file. This reads `config_file`, yielding each non-empty line with whitespace and comments stripped off. """ with open(config_file, 'rt', encoding='utf-8') as f: yield from transducer.lazy.transduce(CONFIG_FILE_PARSER, f)
def test_adding_reducer(self): result = transduce( transducer=mapping(lambda x: x * x), reducer=adding(), iterable=list(range(3)) * 2) self.assertListEqual(list(result), [0, 1, 4])
def test_mapping(self): result = transduce(transducer=mapping(lambda x: x*x), reducer=appending(), iterable=range(5)) self.assertListEqual(result, [0, 1, 4, 9, 16])
def test_mapping(self): result = transduce(transducer=mapping(lambda x: x * x), reducer=appending(), iterable=range(5)) self.assertListEqual(result, [0, 1, 4, 9, 16])
def create2(): Observable.empty().transduce( compose( filtering(even), mapping(mul10)) ).subscribe(noop, noop, throw_error)
def test_exactly_one_item(self): result = transduce(mapping(lambda x: x*x), expecting_single(), [42]) self.assertEqual(result, 1764)
def create(): Observable.throw(RxException()).transduce( compose(filtering(even), mapping(mul10))).subscribe_(noop, throw_error)
from transducer.functional import compose import transducer.lazy from transducer.transducers import filtering, mapping import cosmic_ray.commands import cosmic_ray.counting import cosmic_ray.modules import cosmic_ray.worker from cosmic_ray.testing.test_runner import TestOutcome from cosmic_ray.timing import Timer from cosmic_ray.util import redirect_stdout from cosmic_ray.work_db import use_db, WorkDB LOG = logging.getLogger() REMOVE_COMMENTS = mapping(lambda x: x.split('#')[0]) REMOVE_WHITESPACE = mapping(str.strip) NON_EMPTY = filtering(bool) CONFIG_FILE_PARSER = compose(REMOVE_COMMENTS, REMOVE_WHITESPACE, NON_EMPTY) def _load_file(config_file): """Read configuration from a file. This reads `config_file`, yielding each non-empty line with whitespace and comments stripped off. """ with open(config_file, 'rt', encoding='utf-8') as f: yield from transducer.lazy.transduce(CONFIG_FILE_PARSER, f)
def test_adding_reducer(self): result = transduce(transducer=mapping(lambda x: x * x), reducer=adding(), iterable=list(range(3)) * 2) self.assertListEqual(list(result), [0, 1, 4])