def test_register_kwargs(self): name = 'fedcba' csv.register_dialect(name, delimiter=';') try: self.assertTrue(csv.get_dialect(name).delimiter, '\t') self.assertTrue(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z']) finally: csv.unregister_dialect(name)
def test_register_kwargs(self): name = 'fedcba' csv.register_dialect(name, delimiter=';') try: self.assertNotEqual(csv.get_dialect(name).delimiter, '\t') self.assertEqual(list(csv.reader([b'X;Y;Z'], name)), [[u'X', u'Y', u'Z']]) finally: csv.unregister_dialect(name)
def test_registry(self): class myexceltsv(csv.excel): delimiter = "\t" name = "myexceltsv" expected_dialects = csv.list_dialects() + [name] expected_dialects.sort() csv.register_dialect(name, myexceltsv) try: self.assertTrue(csv.get_dialect(name).delimiter, '\t') got_dialects = csv.list_dialects() got_dialects.sort() self.assertEqual(expected_dialects, got_dialects) finally: csv.unregister_dialect(name)
def test_registry(self): class myexceltsv(csv.excel): delimiter = "\t" name = "myexceltsv" expected_dialects = csv.list_dialects() + [name] expected_dialects.sort() csv.register_dialect(name, myexceltsv) try: self.assertEqual(csv.get_dialect(name).delimiter, '\t') got_dialects = csv.list_dialects() got_dialects.sort() self.assertEqual(expected_dialects, got_dialects) finally: csv.unregister_dialect(name)
def test_dialect_apply(self): class testA(csv.excel): delimiter = "\t" class testB(csv.excel): delimiter = ":" class testC(csv.excel): delimiter = "|" csv.register_dialect('testC', testC) try: fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj) writer.writerow([1,2,3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1,2,3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, testA) writer.writerow([1,2,3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1\t2\t3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, dialect=testB()) writer.writerow([1,2,3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1:2:3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, dialect='testC') writer.writerow([1,2,3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1|2|3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, dialect=testA, delimiter=';') writer.writerow([1,2,3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1;2;3\r\n") finally: fileobj.close() os.unlink(name) finally: csv.unregister_dialect('testC')
def test_dialect_apply(self): class testA(csv.excel): delimiter = "\t" class testB(csv.excel): delimiter = ":" class testC(csv.excel): delimiter = "|" csv.register_dialect('testC', testC) try: fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj) writer.writerow([1, 2, 3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1,2,3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, testA) writer.writerow([1, 2, 3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1\t2\t3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, dialect=testB()) writer.writerow([1, 2, 3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1:2:3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, dialect='testC') writer.writerow([1, 2, 3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1|2|3\r\n") finally: fileobj.close() os.unlink(name) fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj, dialect=testA, delimiter=';') writer.writerow([1, 2, 3]) fileobj.seek(0) self.assertEqual(fileobj.read(), "1;2;3\r\n") finally: fileobj.close() os.unlink(name) finally: csv.unregister_dialect('testC')