mirror of
				https://github.com/KevinMidboe/tableprint.git
				synced 2025-10-29 18:00:16 +00:00 
			
		
		
		
	Merge pull request #2 from nirum/variable_width
Adds support for variable widths across columns
This commit is contained in:
		
							
								
								
									
										1
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								setup.py
									
									
									
									
									
								
							| @@ -17,7 +17,6 @@ setup( | |||||||
|     author_email=metadata['author_email'], |     author_email=metadata['author_email'], | ||||||
|  |  | ||||||
|     license=metadata['license'], |     license=metadata['license'], | ||||||
|  |  | ||||||
|     description=metadata['description'], |     description=metadata['description'], | ||||||
|     long_description='''Formatted console printing of tabular data. |     long_description='''Formatted console printing of tabular data. | ||||||
|                         tableprint lets you easily print formatted tables of data. |                         tableprint lets you easily print formatted tables of data. | ||||||
|   | |||||||
| @@ -19,7 +19,7 @@ import numpy as np | |||||||
| from six import string_types | from six import string_types | ||||||
|  |  | ||||||
| from .style import LineStyle, STYLES | from .style import LineStyle, STYLES | ||||||
| from .utils import ansi_len, format_line | from .utils import ansi_len, format_line, parse_width | ||||||
|  |  | ||||||
| __all__ = ('table', 'header', 'row', 'hrule', 'top', 'bottom', 'banner', 'dataframe') | __all__ = ('table', 'header', 'row', 'hrule', 'top', 'bottom', 'banner', 'dataframe') | ||||||
|  |  | ||||||
| @@ -42,7 +42,7 @@ def table(data, headers=None, format_spec=FMT, width=WIDTH, style=STYLE, out=sys | |||||||
|     format_spec : string, optional |     format_spec : string, optional | ||||||
|         Format specification for formatting numbers (Default: '5g') |         Format specification for formatting numbers (Default: '5g') | ||||||
|  |  | ||||||
|     width : int, optional |     width : int or array_like, optional | ||||||
|         The width of each column in the table (Default: 11) |         The width of each column in the table (Default: 11) | ||||||
|  |  | ||||||
|     style : string or tuple, optional |     style : string or tuple, optional | ||||||
| @@ -53,17 +53,18 @@ def table(data, headers=None, format_spec=FMT, width=WIDTH, style=STYLE, out=sys | |||||||
|     """ |     """ | ||||||
|     ncols = len(data[0]) if headers is None else len(headers) |     ncols = len(data[0]) if headers is None else len(headers) | ||||||
|     tablestyle = STYLES[style] |     tablestyle = STYLES[style] | ||||||
|  |     widths = parse_width(width, ncols) | ||||||
|  |  | ||||||
|     # Initialize with a hr or the header |     # Initialize with a hr or the header | ||||||
|     tablestr = [hrule(ncols, width, tablestyle.top)] \ |     tablestr = [hrule(ncols, widths, tablestyle.top)] \ | ||||||
|         if headers is None else [header(headers, width, style)] |         if headers is None else [header(headers, widths, style)] | ||||||
|  |  | ||||||
|     # parse each row |     # parse each row | ||||||
|     tablestr += [row(d, width, format_spec, style) for d in data] |     tablestr += [row(d, widths, format_spec, style) for d in data] | ||||||
|  |  | ||||||
|     # only add the final border if there was data in the table |     # only add the final border if there was data in the table | ||||||
|     if len(data) > 0: |     if len(data) > 0: | ||||||
|         tablestr += [hrule(ncols, width, tablestyle.bottom)] |         tablestr += [hrule(ncols, widths, tablestyle.bottom)] | ||||||
|  |  | ||||||
|     # print the table |     # print the table | ||||||
|     out.write('\n'.join(tablestr) + '\n') |     out.write('\n'.join(tablestr) + '\n') | ||||||
| @@ -90,16 +91,17 @@ def header(headers, width=WIDTH, style=STYLE, add_hr=True): | |||||||
|         A string consisting of the full header row to print |         A string consisting of the full header row to print | ||||||
|     """ |     """ | ||||||
|     tablestyle = STYLES[style] |     tablestyle = STYLES[style] | ||||||
|  |     widths = parse_width(width, len(headers)) | ||||||
|  |  | ||||||
|     # string formatter |     # string formatter | ||||||
|     data = map(lambda x: ('{:^%d}' % (width + ansi_len(x))).format(x), headers) |     data = map(lambda x: ('{:^%d}' % (x[0] + ansi_len(x[1]))).format(x[1]), zip(widths, headers)) | ||||||
|  |  | ||||||
|     # build the formatted str |     # build the formatted str | ||||||
|     headerstr = format_line(data, tablestyle.row) |     headerstr = format_line(data, tablestyle.row) | ||||||
|  |  | ||||||
|     if add_hr: |     if add_hr: | ||||||
|         upper = hrule(len(headers), width, tablestyle.top) |         upper = hrule(len(headers), widths, tablestyle.top) | ||||||
|         lower = hrule(len(headers), width, tablestyle.below_header) |         lower = hrule(len(headers), widths, tablestyle.below_header) | ||||||
|         headerstr = '\n'.join([upper, headerstr, lower]) |         headerstr = '\n'.join([upper, headerstr, lower]) | ||||||
|  |  | ||||||
|     return headerstr |     return headerstr | ||||||
| @@ -128,6 +130,7 @@ def row(values, width=WIDTH, format_spec=FMT, style=STYLE): | |||||||
|         A string consisting of the full row of data to print |         A string consisting of the full row of data to print | ||||||
|     """ |     """ | ||||||
|     tablestyle = STYLES[style] |     tablestyle = STYLES[style] | ||||||
|  |     widths = parse_width(width, len(values)) | ||||||
|  |  | ||||||
|     assert isinstance(format_spec, string_types) | isinstance(format_spec, list), \ |     assert isinstance(format_spec, string_types) | isinstance(format_spec, list), \ | ||||||
|         "format_spec must be a string or list of strings" |         "format_spec must be a string or list of strings" | ||||||
| @@ -139,7 +142,7 @@ def row(values, width=WIDTH, format_spec=FMT, style=STYLE): | |||||||
|     def mapdata(val): |     def mapdata(val): | ||||||
|  |  | ||||||
|         # unpack |         # unpack | ||||||
|         datum, prec = val |         width, datum, prec = val | ||||||
|  |  | ||||||
|         if isinstance(datum, string_types): |         if isinstance(datum, string_types): | ||||||
|             return ('{:>%i}' % (width + ansi_len(datum))).format(datum) |             return ('{:>%i}' % (width + ansi_len(datum))).format(datum) | ||||||
| @@ -151,7 +154,7 @@ def row(values, width=WIDTH, format_spec=FMT, style=STYLE): | |||||||
|             raise ValueError('Elements in the values array must be strings, ints, or floats') |             raise ValueError('Elements in the values array must be strings, ints, or floats') | ||||||
|  |  | ||||||
|     # string formatter |     # string formatter | ||||||
|     data = map(mapdata, zip(values, format_spec)) |     data = map(mapdata, zip(widths, values, format_spec)) | ||||||
|  |  | ||||||
|     # build the row string |     # build the row string | ||||||
|     return format_line(data, tablestyle.row) |     return format_line(data, tablestyle.row) | ||||||
| @@ -177,7 +180,9 @@ def hrule(n=1, width=WIDTH, linestyle=LineStyle('', '─', '─', '')): | |||||||
|     rowstr : string |     rowstr : string | ||||||
|         A string consisting of the row border to print |         A string consisting of the row border to print | ||||||
|     """ |     """ | ||||||
|     hrstr = linestyle.sep.join([('{:%s^%i}' % (linestyle.hline, width)).format('')] * n) |     widths = parse_width(width, n) | ||||||
|  |     hrstr = linestyle.sep.join([('{:%s^%i}' % (linestyle.hline, width)).format('') | ||||||
|  |                                 for width in widths]) | ||||||
|     return linestyle.begin + hrstr + linestyle.end |     return linestyle.begin + hrstr + linestyle.end | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -74,3 +74,21 @@ def ansi_len(string): | |||||||
| def format_line(data, linestyle): | def format_line(data, linestyle): | ||||||
|     """Formats a list of elements using the given line style""" |     """Formats a list of elements using the given line style""" | ||||||
|     return linestyle.begin + linestyle.sep.join(data) + linestyle.end |     return linestyle.begin + linestyle.sep.join(data) + linestyle.end | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def parse_width(width, n): | ||||||
|  |     """Parses an int or array of widths | ||||||
|  |  | ||||||
|  |     Parameters | ||||||
|  |     ---------- | ||||||
|  |     width : int or array_like | ||||||
|  |     n : int | ||||||
|  |     """ | ||||||
|  |     if isinstance(width, int): | ||||||
|  |         widths = [width] * n | ||||||
|  |  | ||||||
|  |     else: | ||||||
|  |         assert len(width) == n, "Widths and data do not match" | ||||||
|  |         widths = width | ||||||
|  |  | ||||||
|  |     return widths | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user