def test_load_ignore_error(): parser = csv.create_line_parser( dtype=[ ("foo", "int"), ("bar", "str"), ("buzz", "bool"), ], ignore_error=True, ) actual_data = [] error = None def on_error(e): nonlocal error error = e source = rx.from_([ "a,the,True", "42,the,True", "07", ]) source.pipe(csv.load(parser)).subscribe( on_next=actual_data.append, on_error=on_error, ) assert error is None assert len(actual_data) == 1 assert actual_data[0] == (42, 'the', True)
def test_load_quoted(): parser = csv.create_line_parser(dtype=[ ("foo", "int"), ("bar", "str"), ]) actual_data = process( rx.from_([ '1,"the, quick"', '2,"\\"brown fox\\""', '3,"a\"$#ܟ<a;.b^F ^M^E^Aa^Bov^D^\"[^BƆm^A^Q^]#lx"', '4,""', '5,"\\"a\\",b"', '6,",ab"', '7,","', ]), [csv.load(parser)]) assert len(actual_data) == 7 assert actual_data[0] == (1, 'the, quick') assert actual_data[1] == (2, '"brown fox"') assert actual_data[2] == (3, 'a"$#ܟ<a;.b^F ^M^E^Aa^Bov^D^"[^BƆm^A^Q^]#lx') assert actual_data[3] == (4, '') assert actual_data[4] == (5, '"a",b') assert actual_data[5] == (6, ',ab') assert actual_data[6] == (7, ',')
def test_parser_invalid_numbers(): parser = csv.create_line_parser(dtype=[ ("foo", "int"), ("bar", "float"), ]) with pytest.raises(ValueError): process(rx.from_([ "as,ds", ]), [ops.map(parser)])
def test_parser_empty_numbers(): parser = csv.create_line_parser(dtype=[ ("foo", "int"), ("bar", "float"), ]) actual_data = process(rx.from_([ ",", ]), [ops.map(parser)]) assert len(actual_data) == 1 assert actual_data[0] == (None, None)
def test_parser(): parser = csv.create_line_parser(dtype=[ ("foo", "int"), ("bar", "str"), ("buzz", "bool"), ]) actual_data = process(rx.from_([ "42,the,True", "07,quick,False", ]), [ops.map(parser)]) assert len(actual_data) == 2 assert actual_data[0] == (42, 'the', True) assert actual_data[1] == (7, 'quick', False)
def test_parser_typed(): class TestLine(typing.NamedTuple): foo: int bar: str buzz: bool parser = csv.create_line_parser(dtype=TestLine) actual_data = process(rx.from_([ "42,the,True", "07,quick,False", ]), [ops.map(parser)]) assert len(actual_data) == 2 assert actual_data[0] == (42, 'the', True) assert actual_data[1] == (7, 'quick', False)
def test_take_5_lines(): parser = csv.create_line_parser(dtype=[ ('name', 'str'), ('value', 'int'), ]) data = [] csv.load_from_file("tests/sample.csv", parse_line=parser).pipe( rs.ops.take(5), ).subscribe(on_next=data.append) assert len(data) == 5 assert data[0] == ('foo', 1) assert data[1] == ('bar', 2) assert data[2] == ('biz', 3) assert data[3] == ('buz', 4) assert data[4] == ('fiz', 5)
def test_load_from_file(): parser = csv.create_line_parser(dtype=[ ("foo", "int"), ("bar", "str"), ("buzz", "bool"), ]) with tempfile.NamedTemporaryFile(mode='w') as f: f.write("foo,bar,buzz\n") f.write("42,the,True\n") f.write("07,quick,False") f.flush() actual_data = process( csv.load_from_file(f.name, parser), None, ) assert len(actual_data) == 2 assert actual_data[0] == (42, 'the', True) assert actual_data[1] == (7, 'quick', False)
parser = csv.create_line_parser( dtype=[ ('time', 'int'), ('use', 'float'), ('gen', 'float'), ('house_overall', 'float'), ('dishwasher', 'float'), ('furnace1', 'float'), ('furnace2', 'float'), ('home_office', 'float'), ('fridge', 'float'), ('wine_cellar', 'float'), ('garage_door', 'float'), ('kitchen_12', 'float'), ('kitchen_14', 'float'), ('kitchen_38', 'float'), ('barn', 'float'), ('well', 'float'), ('microwave', 'float'), ('living_room', 'float'), ('solar', 'float'), ('temperature', 'float'), ('icon', 'str'), ('humidity', 'float'), ('visibility', 'float'), ('summary', 'str'), ('apparent_temperature', 'float'), ('pressure', 'float'), ('wind_speed', 'float'), ('cloud_cover', 'str'), ('wind_bearing', 'float'), ('precip_intensity', 'float'), ('dew_point', 'float'), ('precip_probability', 'float'), ] )