summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--prettytable/prettytable.py115
-rw-r--r--tests/test_prettytable.py32
2 files changed, 120 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 #
##############################
diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py
index 6d8c573..064e2b0 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_md_string()
+ 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_string()
+ print()
+ print(result)
+ self.assertEqual(result.strip(), """
++----+----+----+
+| A | B | C |
++====+====+====+
+| a | b | c |
++----+----+----+
+| aa | bb | cc |
++----+----+----+
+""".strip())
+
+
if __name__ == "__main__":
unittest.main()