diff options
author | Kane Blueriver <kxxoling@gmail.com> | 2018-07-30 22:58:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-30 22:58:53 +0800 |
commit | bcfdb92811ae1f39e1065f31544710bf87d3bc21 (patch) | |
tree | 4ea8e8f88f60ea23a9549bba1c0ea09343f0c8ce /prettytable/prettytable.py | |
parent | 46898fa095a2e24cd0551fe2ff6a3b80d5db3063 (diff) | |
parent | 1c78b292c8581d1d4ebf8d2b59f114dbde77c705 (diff) | |
download | python-prettytable-ptable-develop.tar.gz |
Support markdown output string and reStructuredText output string
Diffstat (limited to 'prettytable/prettytable.py')
-rw-r--r-- | prettytable/prettytable.py | 115 |
1 files changed, 88 insertions, 27 deletions
diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index dae13f5..02dab66 100644 --- a/prettytable/prettytable.py +++ b/prettytable/prettytable.py @@ -1144,32 +1144,11 @@ class PrettyTable(object): # PLAIN TEXT STRING METHODS # ############################## - def get_string(self, **kwargs): - - """Return string representation of table in current state. + def _prepare_lines(self, **kwargs): - Arguments: - - title - optional table title - start - index of first data row to include in output - end - index of last data row to include in output PLUS ONE (list slice style) - fields - names of fields (columns) to include - header - print a header showing field names (True or False) - border - print a border around the table (True or False) - hrules - controls printing of horizontal rules after rows. Allowed values: ALL, FRAME, HEADER, NONE - vrules - controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE - int_format - controls formatting of integer data - float_format - controls formatting of floating point data - padding_width - number of spaces on either side of column data (only used if left and right paddings are None) - left_padding_width - number of spaces on left hand side of column data - right_padding_width - number of spaces on right hand side of column data - vertical_char - single character string used to draw vertical lines - horizontal_char - single character string used to draw horizontal lines - junction_char - single character string used to draw line junctions - sortby - name of field to sort rows by - sort_key - sorting key function, applied to data points before sorting - reversesort - True or False to sort in descending or ascending order - print empty - if True, stringify just the header for an empty table, if False return an empty string """ + """Return a list of lines, for building the return-string of `get_string`. + Arguments: see method `get_string`. + """ options = self._get_options(kwargs) @@ -1178,7 +1157,7 @@ class PrettyTable(object): # Don't think too hard about an empty table # Is this the desired behaviour? Maybe we should still print the header? if self.rowcount == 0 and (not options["print_empty"] or not options["border"]): - return "" + return [] # Get the rows we need to print, taking into account slicing, sorting, etc. rows = self._get_rows(options) @@ -1197,7 +1176,7 @@ class PrettyTable(object): # Add header or top of border if options["header"]: - lines.append(self._stringify_header(options)) + lines.extend(self._stringify_header(options).split('\n')) elif options["border"] and options["hrules"] in (ALL, FRAME): lines.append(self._hrule) @@ -1208,7 +1187,35 @@ class PrettyTable(object): # Add bottom of border if options["border"] and options["hrules"] == FRAME: lines.append(self._hrule) + return lines + + def get_string(self, **kwargs): + """Return string representation of table in current state. + + Arguments: + + title - optional table title + start - index of first data row to include in output + end - index of last data row to include in output PLUS ONE (list slice style) + fields - names of fields (columns) to include + header - print a header showing field names (True or False) + border - print a border around the table (True or False) + hrules - controls printing of horizontal rules after rows. Allowed values: ALL, FRAME, HEADER, NONE + vrules - controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE + int_format - controls formatting of integer data + float_format - controls formatting of floating point data + padding_width - number of spaces on either side of column data (only used if left and right paddings are None) + left_padding_width - number of spaces on left hand side of column data + right_padding_width - number of spaces on right hand side of column data + vertical_char - single character string used to draw vertical lines + horizontal_char - single character string used to draw horizontal lines + junction_char - single character string used to draw line junctions + sortby - name of field to sort rows by + sort_key - sorting key function, applied to data points before sorting + reversesort - True or False to sort in descending or ascending order + print empty - if True, stringify just the header for an empty table, if False return an empty string """ + lines = self._prepare_lines(**kwargs) return self._unicode("\n").join(lines) def _stringify_hrule(self, options): @@ -1389,6 +1396,60 @@ class PrettyTable(object): kwargs["start"] += page_length return "\f".join(pages) + ################################ + # MARKDOWN TEXT STRING METHODS # + ################################ + + def get_md_string(self, **kwargs): + + """Return string representation of table in markdown. + + Arguments: + + start - index of first data row to include in output + end - index of last data row to include in output PLUS ONE (list slice style) + fields - names of fields (columns) to include + int_format - controls formatting of integer data + float_format - controls formatting of floating point data + sortby - name of field to sort rows by + sort_key - sorting key function, applied to data points before sorting + reversesort - True or False to sort in descending or ascending order + print empty - if True, stringify just the header for an empty table, if False return an empty string """ + + kwargs['title'] = None + kwargs['header'] = True + kwargs['border'] = True + kwargs['junction_char'] = '|' + kwargs['hrules'] = HEADER + lines = self._prepare_lines(**kwargs) + return self._unicode("\n").join(lines) + + def get_rst_string(self, **kwargs): + + """Return string representation of table in reStructuredText. + + Arguments: + + start - index of first data row to include in output + end - index of last data row to include in output PLUS ONE (list slice style) + fields - names of fields (columns) to include + int_format - controls formatting of integer data + float_format - controls formatting of floating point data + sortby - name of field to sort rows by + sort_key - sorting key function, applied to data points before sorting + reversesort - True or False to sort in descending or ascending order + print empty - if True, stringify just the header for an empty table, if False return an empty string """ + + kwargs['title'] = None + kwargs['header'] = True + kwargs['border'] = True + kwargs['hrules'] = ALL + lines = self._prepare_lines(**kwargs) + # line-0 is _hrule, line-1 is header, line-2 is _hrule + if len(lines) >= 3: + lines[2] = lines[2].replace('-', '=') + return self._unicode("\n").join(lines) + ############################## # HTML STRING METHODS # ############################## |