Example #1
0
    def test_iter_tsv(self):
        path = tempfile.mktemp()

        with open(path, 'wb') as handle:
            write_tsv(handle, 'A', 'B', 'C')

        with open(path, 'rb') as handle:
            row = next(iter_tsv(handle))
            self.assertEqual(3, len(row))

        with open(path, 'rb') as handle:
            row = next(iter_tsv(handle, cols=('a', 'b', 'c')))
            self.assertEqual(3, len(row))
            self.assertEqual(row.a, 'A')
            self.assertEqual(row.b, 'B')
            self.assertEqual(row.c, 'C')

        with open(path, 'rb') as handle:
            exception_raised = False
            try:
                row = next(iter_tsv(handle, cols=('a', 'b')))
            except TypeError:
                exception_raised = True
            self.assertTrue(exception_raised)

        with open(path, 'rb') as handle:
            row = next(iter_tsv(handle, cols=('a', 0, 0)))
            self.assertEqual(3, len(row))
            self.assertEqual(row.a, 'A')
            self.assertFalse(hasattr(row, 'b'))
            self.assertFalse(hasattr(row, 'c'))

        with open(path, 'rb') as handle:
            row = next(iter_tsv(handle, cols=('X', 'b', 'X')))
            self.assertEqual(3, len(row))
            self.assertEqual(row.b, 'B')
            self.assertFalse(hasattr(row, 'a'))
            self.assertFalse(hasattr(row, 'c'))
Example #2
0
    def test_iter_tsv(self):
        path = tempfile.mktemp()

        with open(path, 'w') as handle:
            write_tsv(handle, 'A', 'B', 'C')

        with open(path) as handle:
            row = iter_tsv(handle).next()
            self.assertEqual(3, len(row))

        with open(path) as handle:
            row = iter_tsv(handle, cols=('a', 'b', 'c')).next()
            self.assertEqual(3, len(row))
            self.assertEqual(row.a, 'A')
            self.assertEqual(row.b, 'B')
            self.assertEqual(row.c, 'C')

        with open(path) as handle:
            exception_raised = False
            try:
                row = iter_tsv(handle, cols=('a', 'b')).next()
            except TypeError:
                exception_raised = True
            self.assertTrue(exception_raised)

        with open(path) as handle:
            row = iter_tsv(handle, cols=('a', 0, 0)).next()
            self.assertEqual(3, len(row))
            self.assertEqual(row.a, 'A')
            self.assertFalse(hasattr(row, 'b'))
            self.assertFalse(hasattr(row, 'c'))

        with open(path) as handle:
            row = iter_tsv(handle, cols=('X', 'b', 'X')).next()
            self.assertEqual(3, len(row))
            self.assertEqual(row.b, 'B')
            self.assertFalse(hasattr(row, 'a'))
            self.assertFalse(hasattr(row, 'c'))
Example #3
0
 def test_write_tsv(self):
     path = tempfile.mktemp()
     with open(path, 'wb') as handle:
         write_tsv(handle, 'A', 'B', 'C')
     with open(path, 'rb') as handle:
         self.assertEqual(b'A\tB\tC\n', handle.read())
Example #4
0
 def test_write_tsv(self):
     path = tempfile.mktemp()
     with open(path, 'w') as handle:
         write_tsv(handle, 'A', 'B', 'C')
     with open(path) as handle:
         self.assertEqual('A\tB\tC\n', handle.read())