示例#1
0
def test_unsupported_dtype_gives_error():
    reader = sample_reader()

    with pytest.raises(ValueError) as excinfo:
        # Remember, it won't yield anything if we don't "next" it
        next(read_csv_records(reader, dtypes={"something": "no-such-dtype"}))
    assert "Unrecognized dtype 'no-such-dtype'" in str(excinfo.value)
示例#2
0
def test_basic_read_csv_records():
    """The base test of read_csv_records - no customisation.
    """
    reader = sample_reader()

    result = list(read_csv_records(reader))

    assert result == [
        {
            "time": 100,
            "col1": 1,
            "col2": 10,
            "col3": 1.0,
            "col4": "abcd",
            "col5": True,
            "col6": None,
        },
        {
            "time": 200,
            "col1": 2,
            "col2": 20,
            "col3": 2.0,
            "col4": "efgh",
            "col5": False,
            "col6": None,
        },
    ]
示例#3
0
def test_dtypes_plus_converters_change_parsing():
    reader = sample_reader()

    result = list(
        read_csv_records(
            reader, dtypes={"col1": "str", "col6": "str",}, converters={"col2": float,}
        )
    )

    assert result == [
        {
            "time": 100,
            "col1": "0001",
            "col2": 10.0,
            "col3": 1.0,
            "col4": "abcd",
            "col5": True,
            "col6": "none",
        },
        {
            "time": 200,
            "col1": "0002",
            "col2": 20.0,
            "col3": 2.0,
            "col4": "efgh",
            "col5": False,
            "col6": "",
        },
    ]
示例#4
0
    def _read_csv_file(self,
                       file_like,
                       dialect=csv.excel,
                       columns=None,
                       encoding="utf-8",
                       dtypes=None,
                       converters=None,
                       **kwargs):
        if columns is None:
            reader = csv_dict_record_reader(file_like, encoding, dialect)
        else:
            reader = csv_text_record_reader(file_like, encoding, dialect,
                                            columns)

        return read_csv_records(reader, dtypes, converters, **kwargs)
示例#5
0
def test_guess_dtype_gives_default_result():
    reader = sample_reader()

    result = list(
        read_csv_records(
            reader,
            dtypes={
                "time": "guess",
                "col1": "guess",
                "col2": "guess",
                "col3": "guess",
                "col4": "guess",
                "col5": "guess",
                "col6": "guess",
            },
        )
    )

    assert result == [
        {
            "time": 100,
            "col1": 1,
            "col2": 10,
            "col3": 1.0,
            "col4": "abcd",
            "col5": True,
            "col6": None,
        },
        {
            "time": 200,
            "col1": 2,
            "col2": 20,
            "col3": 2.0,
            "col4": "efgh",
            "col5": False,
            "col6": None,
        },
    ]
示例#6
0
def test_dtypes_overridden_by_converters():
    reader = sample_reader()

    result = list(
        read_csv_records(
            reader,
            dtypes={
                "time": "bool",  # overridden by converters
                "col1": "str",
                "col2": "int",  # overridden by converters
                "col6": "str",
            },
            converters={"time": int, "col2": float, "col5": str,},
        )
    )

    assert result == [
        {
            "time": 100,
            "col1": "0001",
            "col2": 10.0,
            "col3": 1.0,
            "col4": "abcd",
            "col5": "true",
            "col6": "none",
        },
        {
            "time": 200,
            "col1": "0002",
            "col2": 20.0,
            "col3": 2.0,
            "col4": "efgh",
            "col5": "false",
            "col6": "",
        },
    ]