Beispiel #1
0
def test_un_with_inconsistent_number_of_fields():
    source = [
        '1',
        '2\t3',
    ]

    # Number of fields of all rows should be equal to number of fields in the
    # first row.
    with pytest.raises(ValueError) as excinfo:
        list(tsv.un(source))
    assert str(excinfo.value) == 'Expected 1 fields in line 2, saw 2'

    # If error_bad_lines is turned off, bad lines are simply skipped.
    with pytest.warns(UserWarning) as warns:
        assert list(tsv.un(source, error_bad_lines=False)) == [['1']]
        assert len(warns) == 1
        assert str(warns[0].message) == 'Expected 1 fields in line 2, saw 2'
Beispiel #2
0
def test_un():
    source = '\n'.join([
        r'1	a',
        r'2	\t',
        r'3	\n',
        r'4	\\',
        r'5	\N',
        r'6	\j',
        r'',
    ])
    assert list(tsv.un(source)) == [
        ['1', 'a'],
        ['2', '\t'],
        ['3', '\n'],
        ['4', '\\'],
        ['5', None],
        ['6', 'j'],
    ]
Beispiel #3
0
def main():
    results = [(pop.location(), pop.density())
               for pop in tsv.un(sys.stdin, Pop) if pop]
    tsv.to(results, sys.stdout)
Beispiel #4
0
 def __iter_extended_rows(self):
     items = tsv.un(self.__chars)
     for number, item in enumerate(items, start=1):
         yield (number, None, list(item))
Beispiel #5
0
def test_final_backslash_error():
    assert list(tsv.un('1\t\\\n')) == [['1', '']]
    assert list(tsv.un('1\t\\z\n')) == [['1', 'z']]
Beispiel #6
0
def test_un_namedtuple_error():
    Row = collections.namedtuple('Row', 'x,y')
    with pytest.raises(ValueError):
        list(tsv.un('1\n', Row))
    with pytest.raises(ValueError):
        list(tsv.un('1\ta\tb\n', Row))
Beispiel #7
0
def test_un_namedtuple_row_holder():
    Row = collections.namedtuple('Row', 'x,y')
    assert list(tsv.un('1\ta\n', Row)) == [Row(x='1', y='a')]
Beispiel #8
0
def test_final_backslash_error():
    with pytest.raises(tsv.FinalBackslashInFieldIsForbidden):
        list(tsv.un('1\t\\\n'))