Skip to content

Commit cbf9899

Browse files
committed
1 parent 428cb65 commit cbf9899

File tree

7 files changed

+109
-16
lines changed

7 files changed

+109
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ dist
99
docs/_build
1010
.coverage
1111
.tox
12+
cover

csvkit/utilities/csvclean.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CSVClean(CSVKitUtility):
1111

1212
def add_arguments(self):
1313
self.argparser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true',
14-
help='If this argument is present, no output will be created. Information about what would have been done will be printed to STDERR.''')
14+
help='Do not create output files. Information about what would have been done will be printed to STDERR.')
1515

1616
def main(self):
1717
reader = CSVKitReader(self.args.file, **self.reader_kwargs)
@@ -22,15 +22,15 @@ def main(self):
2222
pass
2323
if checker.errs:
2424
for e in checker.errs:
25-
self.output_file.write("Line %i: %s\n" % (e.line_number,e.msg))
25+
self.output_file.write('Line %i: %s\n' % (e.line_number,e.msg))
2626
else:
27-
self.output_file.write("No errors.\n")
27+
self.output_file.write('No errors.\n')
2828
if checker.joins:
29-
self.output_file.write("%i rows would have been joined/reduced to %i rows after eliminating expected internal line breaks.\n" % (checker.rows_joined, checker.joins))
29+
self.output_file.write('%i rows would have been joined/reduced to %i rows after eliminating expected internal line breaks.\n' % (checker.rows_joined, checker.joins))
3030
else:
3131
base,ext = splitext(self.args.file.name)
3232
# should we preserve delimiters and other dialect args from CLI?
33-
cleaned_file = CSVKitWriter(open("%s_out.csv" % base,"w"), **self.writer_kwargs)
33+
cleaned_file = CSVKitWriter(open('%s_out.csv' % base,'w'), **self.writer_kwargs)
3434

3535
checker = RowChecker(reader)
3636
cleaned_file.writerow(checker.column_names)
@@ -39,23 +39,22 @@ def main(self):
3939

4040
if checker.errs:
4141
# should we preserve delimiters and other dialect args from CLI?
42-
err_filename = "%s_err.csv" % base
43-
err_file = CSVKitWriter(open(err_filename, "w"), **self.writer_kwargs)
42+
err_filename = '%s_err.csv' % base
43+
err_file = CSVKitWriter(open(err_filename, 'w'), **self.writer_kwargs)
4444
err_header = ['line_number','msg']
4545
err_header.extend(checker.column_names)
4646
err_file.writerow(err_header)
4747
for e in checker.errs:
4848
err_file.writerow(self._format_error_row(e))
4949
err_count = len(checker.errs)
50-
self.output_file.write("%i error%s logged to %s\n" % (err_count,"" if err_count == 1 else "s", err_filename))
50+
self.output_file.write('%i error%s logged to %s\n' % (err_count,'' if err_count == 1 else 's', err_filename))
5151
else:
52-
self.output_file.write("No errors.\n")
52+
self.output_file.write('No errors.\n')
5353

5454
if checker.joins:
55-
self.output_file.write("%i rows were joined/reduced to %i rows after eliminating expected internal line breaks.\n" % (checker.rows_joined, checker.joins))
55+
self.output_file.write('%i rows were joined/reduced to %i rows after eliminating expected internal line breaks.\n' % (checker.rows_joined, checker.joins))
5656

5757
def _format_error_row(self, e):
58-
"""Format a row for """
5958
err_row = [e.line_number, e.msg]
6059
err_row.extend(e.row)
6160
return err_row
@@ -64,6 +63,6 @@ def launch_new_instance():
6463
utility = CSVClean()
6564
utility.main()
6665

67-
if __name__ == "__main__":
66+
if __name__ == '__main__':
6867
launch_new_instance()
6968

examples/join_a.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a,b,c
2+
1,b,c
3+
2,b,c
4+
3,b,c

examples/join_b.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a,b,c
2+
1,b,c
3+
1,b,c
4+
4,b,c
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
#!/usr/bin/env python
22

3+
from cStringIO import StringIO
4+
import os
35
import unittest
46

57
from csvkit.utilities.csvclean import CSVClean
68

79
class TestCSVClean(unittest.TestCase):
8-
pass
10+
def test_simple(self):
11+
args = ['examples/bad.csv']
12+
output_file = StringIO()
13+
14+
utility = CSVClean(args, output_file)
15+
utility.main()
16+
17+
self.assertTrue(os.path.exists('examples/bad_err.csv'))
18+
self.assertTrue(os.path.exists('examples/bad_out.csv'))
19+
20+
with open('examples/bad_err.csv') as f:
21+
f.next()
22+
self.assertEqual(f.next()[0], '1')
23+
self.assertEqual(f.next()[0], '2')
24+
self.assertRaises(StopIteration, f.next)
25+
26+
with open('examples/bad_out.csv') as f:
27+
f.next()
28+
self.assertEqual(f.next()[0], '0')
29+
self.assertRaises(StopIteration, f.next)
30+
31+
# Cleanup
32+
os.remove('examples/bad_err.csv')
33+
os.remove('examples/bad_out.csv')
934

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
11
#!/usr/bin/env python
22

3+
from cStringIO import StringIO
34
import unittest
45

5-
from csvkit.utilities.csvjoin import CSVJoin
6+
from csvkit.utilities.csvjoin import CSVJoin
67

78
class TestCSVJoin(unittest.TestCase):
8-
pass
9+
def test_sequential(self):
10+
args = ['examples/join_a.csv', 'examples/join_b.csv']
11+
output_file = StringIO()
12+
13+
utility = CSVJoin(args, output_file)
14+
utility.main()
15+
16+
output = StringIO(output_file.getvalue())
17+
18+
self.assertEqual(len(output.readlines()), 4)
19+
20+
def test_inner(self):
21+
args = ['-c', 'a', 'examples/join_a.csv', 'examples/join_b.csv']
22+
output_file = StringIO()
23+
24+
utility = CSVJoin(args, output_file)
25+
utility.main()
26+
27+
output = StringIO(output_file.getvalue())
28+
29+
self.assertEqual(len(output.readlines()), 3)
30+
31+
def test_left(self):
32+
args = ['-c', 'a', '--left', 'examples/join_a.csv', 'examples/join_b.csv']
33+
output_file = StringIO()
34+
35+
utility = CSVJoin(args, output_file)
36+
utility.main()
37+
38+
output = StringIO(output_file.getvalue())
39+
40+
self.assertEqual(len(output.readlines()), 5)
41+
42+
def test_right(self):
43+
args = ['-c', 'a', '--right', 'examples/join_a.csv', 'examples/join_b.csv']
44+
output_file = StringIO()
45+
46+
utility = CSVJoin(args, output_file)
47+
utility.main()
48+
49+
output = StringIO(output_file.getvalue())
50+
51+
self.assertEqual(len(output.readlines()), 4)
52+
53+
def test_outer(self):
54+
args = ['-c', 'a', '--outer', 'examples/join_a.csv', 'examples/join_b.csv']
55+
output_file = StringIO()
56+
57+
utility = CSVJoin(args, output_file)
58+
utility.main()
59+
60+
output = StringIO(output_file.getvalue())
61+
62+
self.assertEqual(len(output.readlines()), 6)
963

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#!/usr/bin/env python
22

3+
from cStringIO import StringIO
34
import unittest
45

56
from csvkit.utilities.csvstat import CSVStat
67

78
class TestCSVStat(unittest.TestCase):
8-
pass
9+
def test_runs(self):
10+
args = ['examples/dummy.csv']
11+
output_file = StringIO()
12+
13+
utility = CSVStat(args, output_file)
14+
utility.main()
915

0 commit comments

Comments
 (0)