def test_Try(): div = lambda x: 10 // x Div = nut_function(div) assert [] >> Try(div) >> Collect() == [] assert [10, 5, 1] >> Try(div) >> Collect() == [1, 2, 10] assert [10, 0, 1] >> Try(Div(), -1) >> Collect() == [1, -1, 10] assert [10, 0, 1] >> Try(Div(), 'IGNORE') >> Collect() == [1, 10] ifzero = lambda x, e: str(x) assert [10, 0, 1] >> Try(Div(), ifzero) >> Collect() == [1, '0', 10] with Redirect('STDOUT') as out: result = [10, 0, 1] >> Try(div, 'STDOUT') >> Collect() assert result == [1, 10] assert out.getvalue() == 'ERROR: 0 : integer division or modulo by zero\n' with Redirect('STDERR') as out: result = [10, 0, 1] >> Try(div) >> Collect() assert result == [1, 10] assert out.getvalue() == 'ERROR: 0 : integer division or modulo by zero\n' with pytest.raises(TypeError) as ex: [1, 0, 3] >> Try(Map(Div())) >> Collect() assert str(ex.value).startswith("Need (nut) function")
def test_Redirect(): with Redirect() as out: print('test') assert out.getvalue() == 'test\n' with Redirect('STDERR') as out: print('error', file=sys.stderr) assert out.getvalue() == 'error\n'
def test_PrintType(): a = np.zeros((3, 4), dtype='uint8') b = np.zeros((1, 2), dtype='float32') with Redirect() as out: data = [(a, b), 1.1, [[a], 2]] data >> PrintType() >> Consume() assert out.getvalue() == expected_type_info1[1:] with Redirect() as out: data = [1.1, [[a], 2]] data >> PrintType('data:') >> Consume() assert out.getvalue() == expected_type_info2[1:]
def test_PrintColType(): with Redirect() as out1: data = [(np.zeros((10, 20, 3)), 1), ('text', 2), 3] data >> PrintColType() >> Consume() assert out1.getvalue() == expected_col_info1[1:] with Redirect() as out2: data = [(1, 2), (3, 4)] data >> PrintColType(1) >> Consume() assert out2.getvalue() == expected_col_info2[1:] with Redirect() as out3: data = [(1, 2), (3, 4)] data >> PrintColType((0, 1)) >> Consume() assert out3.getvalue() == expected_col_info3[1:]
def test_PrintProgress(): with Redirect() as out: numbers = range(3) numbers >> PrintProgress(numbers, 'nums:', 0) >> Consume() expected = \ '\rnums: 0% \rnums: 50% \rnums: 100% \rnums: 100% \n' assert out.getvalue() == expected
def test_BuildBatch_verbose(): with Redirect() as out: samples = [[1], [2], [3]] build_batch = (nb.BuildBatch(2, verbose=True).input(0, 'number', 'uint8')) samples >> build_batch >> Consume() assert out.getvalue() == '[2:uint8]\n[1:uint8]\n' with Redirect() as out: samples = [(np.array([1, 2, 3]), 0), (np.array([4, 5, 6]), 1), (np.array([7, 8, 9]), 1)] build_batch = (nb.BuildBatch(2, verbose=True).input( 0, 'vector', 'float32').output(1, 'one_hot', 'uint8', 2)) samples >> build_batch >> Consume() expected = '[[2x3:float32], [2x2:uint8]]\n[[1x3:float32], [1x2:uint8]]\n' assert out.getvalue() == expected
def test_print_type(): with Redirect() as out: a = np.zeros((3, 4), dtype='uint8') data = [[a], (1.1, 2)] print_type(data) expected = '[[<ndarray> 3x4:uint8], (<float> 1.1, <int> 2)]\n' assert out.getvalue() == expected
def test_Print(): with Redirect() as _: assert [1, 2, 3] >> Print() >> Collect() == [1, 2, 3] with Redirect() as out: data = [np_array(1), np_array(2)] data >> Print() >> Consume() assert out.getvalue() == '1\n2\n' with Redirect() as out: data = [np_array([1, 2]), np_array([3, 4])] data >> Print('{1}:{0}') >> Consume() assert out.getvalue() == '2:1\n4:3\n' with Redirect() as out: [1, 2, 3] >> Print() >> Consume() assert out.getvalue() == '1\n2\n3\n' with Redirect() as out: xrange(10) >> Print(every_n=3) >> Consume() assert out.getvalue() == '2\n5\n8\n' with Redirect() as out: even = lambda x: x % 2 == 0 [1, 2, 3, 4] >> Print(filterfunc=even) >> Consume() assert out.getvalue() == '2\n4\n' with Redirect() as out: [1, 2, 3, 4] >> Sleep(0.1) >> Print(every_sec=0.15) >> Consume() assert out.getvalue() == '2\n4\n' with Redirect() as out: [1, 2, 3] >> Print('num:{}') >> Consume() assert out.getvalue() == 'num:1\nnum:2\nnum:3\n' with Redirect() as out: fmt = lambda x: 'num:' + str(x) [1, 2, 3] >> Print(fmt) >> Consume() assert out.getvalue() == 'num:1\nnum:2\nnum:3\n' with Redirect() as out: [(1, 2), (3, 4)] >> Print('{1}:{0}') >> Consume() assert out.getvalue() == '2:1\n4:3\n' with pytest.raises(ValueError) as ex: [1, 2, 3] >> Print(['invalid']) >> Consume() assert str(ex.value) == "Invalid format ['invalid']"
def test_console(): with Redirect() as out: console('test') assert out.getvalue() == 'test\n'
def test_Redirect(): with Redirect() as out: print('test') assert out.getvalue() == 'test\n'
def test_run_examples(): with Redirect() as out: examples.run('tests/data/') assert out.getvalue()