From ee0dbe6e4368ab0e93c6353963a242f4566ba416 Mon Sep 17 00:00:00 2001 From: wolfhong Date: Wed, 2 May 2018 18:52:46 +0800 Subject: add method get_markdown for markdown-style table output; add method get_rst for reStructuredText-style table output --- prettytable/prettytable.py | 115 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 27 deletions(-) diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index 1f9e3a1..86f4e6d 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_markdown(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(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 # ############################## -- cgit v1.2.1 From 34b0deead2bbff15c0da730e05564d055fe8590e Mon Sep 17 00:00:00 2001 From: wolfhong Date: Tue, 8 May 2018 20:59:12 +0800 Subject: add test for get_markdown and get_rst --- tests/test_prettytable.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 6d8c573..235cd81 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -727,5 +727,37 @@ g.. """.strip()) +class PrintMarkdownAndRstTest(unittest.TestCase): + def setUp(self): + self.x = PrettyTable(["A", "B", "C"]) + self.x.add_row(["a", "b", "c"]) + self.x.add_row(["aa", "bb", "cc"]) + + def testMarkdownOutput(self): + result = self.x.get_markdown() + print() + print(result) + self.assertEqual(result.strip(), """ +| A | B | C | +|----|----|----| +| a | b | c | +| aa | bb | cc | +""".strip()) + + def testRstOutput(self): + result = self.x.get_rst() + print() + print(result) + self.assertEqual(result.strip(), """ ++----+----+----+ +| A | B | C | ++====+====+====+ +| a | b | c | ++----+----+----+ +| aa | bb | cc | ++----+----+----+ +""".strip()) + + if __name__ == "__main__": unittest.main() -- cgit v1.2.1 From 1c78b292c8581d1d4ebf8d2b59f114dbde77c705 Mon Sep 17 00:00:00 2001 From: wolfhong Date: Mon, 30 Jul 2018 15:51:00 +0800 Subject: rename get_markdown to get_md_string; rename get_rst to get_rst_string; --- prettytable/prettytable.py | 4 ++-- tests/test_prettytable.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index 86f4e6d..96b94bf 100644 --- a/prettytable/prettytable.py +++ b/prettytable/prettytable.py @@ -1400,7 +1400,7 @@ class PrettyTable(object): # MARKDOWN TEXT STRING METHODS # ################################ - def get_markdown(self, **kwargs): + def get_md_string(self, **kwargs): """Return string representation of table in markdown. @@ -1424,7 +1424,7 @@ class PrettyTable(object): lines = self._prepare_lines(**kwargs) return self._unicode("\n").join(lines) - def get_rst(self, **kwargs): + def get_rst_string(self, **kwargs): """Return string representation of table in reStructuredText. diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 235cd81..064e2b0 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -734,7 +734,7 @@ class PrintMarkdownAndRstTest(unittest.TestCase): self.x.add_row(["aa", "bb", "cc"]) def testMarkdownOutput(self): - result = self.x.get_markdown() + result = self.x.get_md_string() print() print(result) self.assertEqual(result.strip(), """ @@ -745,7 +745,7 @@ class PrintMarkdownAndRstTest(unittest.TestCase): """.strip()) def testRstOutput(self): - result = self.x.get_rst() + result = self.x.get_rst_string() print() print(result) self.assertEqual(result.strip(), """ -- cgit v1.2.1