Skip to content

Commit

Permalink
Merge ed0cf13 into c55b80c
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Jan 30, 2016
2 parents c55b80c + ed0cf13 commit e0aa657
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 49 deletions.
35 changes: 1 addition & 34 deletions csvkit/utilities/csvlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,7 @@ def main(self):
column_names.insert(0, 'line_number')
rows = [list(itertools.chain([str(i + 1)], row)) for i, row in enumerate(rows)]

# Convert to normal list of rows
rows = list(rows)

# Insert the column names at the top
rows.insert(0, column_names)

widths = []

for row in rows:
for i, v in enumerate(row):
try:
if len(v) > widths[i]:
widths[i] = len(v)
except IndexError:
widths.append(len(v))

# Dashes span each width with '+' character at intersection of
# horizontal and vertical dividers.
divider = '|--' + '-+-'.join('-' * w for w in widths) + '--|'

self.output_file.write('%s\n' % divider)

for i, row in enumerate(rows):
output = []

for j, d in enumerate(row):
if d is None:
d = ''
output.append(' %s ' % six.text_type(d).ljust(widths[j]))

self.output_file.write('| %s |\n' % ('|'.join(output)))

if (i == 0 or i == len(rows) - 1):
self.output_file.write('%s\n' % divider)
agate.Table(list(rows)).print_table(output=self.output_file)


def launch_new_instance():
Expand Down
2 changes: 1 addition & 1 deletion examples/test_utf8.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
a,b,c
foo,bar,baz
1,2,3
4,5,ʤ
2 changes: 1 addition & 1 deletion tests/test_utilities/test_csvcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_unicode(self):
input_file = six.StringIO(output_file.getvalue())
reader = agate.reader(input_file)

self.assertEqual(next(reader), ['a', 'c'])
self.assertEqual(next(reader), ['foo', 'baz'])
self.assertEqual(next(reader), ['1', '3'])
self.assertEqual(next(reader), ['4', u'ʤ'])

Expand Down
42 changes: 29 additions & 13 deletions tests/test_utilities/test_csvlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_simple(self):

input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| a | b | c |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 1 | 4 | 5 |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '|-------+---+----|\n')
self.assertEqual(next(input_file), '| A | B | C |\n')
self.assertEqual(next(input_file), '|-------+---+----|\n')
self.assertEqual(next(input_file), '| True | 2 | 3 |\n')
self.assertEqual(next(input_file), '| True | 4 | 5 |\n')
self.assertEqual(next(input_file), '|-------+---+----|\n')

def test_no_header(self):
args = ['--no-header-row', 'examples/no_header_row3.csv']
Expand All @@ -46,12 +46,12 @@ def test_no_header(self):

input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----------+---------+----------|\n')
self.assertEqual(next(input_file), '| column1 | column2 | column3 |\n')
self.assertEqual(next(input_file), '|----------+---------+----------|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 4 | 5 | 6 |\n')
self.assertEqual(next(input_file), '|----------+---------+----------|\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| A | B | C |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 4 | 5 | 6 |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')

def test_unicode(self):
args = ['examples/test_utf8.csv']
Expand All @@ -64,8 +64,24 @@ def test_unicode(self):
input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| a | b | c |\n')
self.assertEqual(next(input_file), '| A | B | C |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), u'| 4 | 5 | ʤ |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')

def test_linenumbers(self):
args = ['--linenumbers', 'examples/dummy3.csv']
output_file = six.StringIO()
utility = CSVLook(args, output_file)

utility.main()

input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----+------+---+----|\n')
self.assertEqual(next(input_file), '| A | B | C | D |\n')
self.assertEqual(next(input_file), '|----+------+---+----|\n')
self.assertEqual(next(input_file), '| 1 | True | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 2 | True | 4 | 5 |\n')
self.assertEqual(next(input_file), '|----+------+---+----|\n')

0 comments on commit e0aa657

Please sign in to comment.