summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2013-04-06 23:50:28 +0000
committerluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2013-04-06 23:50:28 +0000
commit86a59f8551fcbc4baed7fc77e28a1214cdbc9211 (patch)
treee96d23944b082ca3adbd64e3e2cfeed5ac1db833
parentbf505c9e08f423f4820c71a9b93ab2d6a2eeeeca (diff)
downloadpython-prettytable-0.7.tar.gz
Backporting print_empty feature from trunk.0.7
git-svn-id: http://prettytable.googlecode.com/svn/branches/0.7@126 0f58610c-415a-11de-9c03-5d6cfad8e937
-rw-r--r--prettytable.py37
-rw-r--r--prettytable_test.py19
2 files changed, 51 insertions, 5 deletions
diff --git a/prettytable.py b/prettytable.py
index 4be4788..8abb952 100644
--- a/prettytable.py
+++ b/prettytable.py
@@ -29,7 +29,7 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
-__version__ = "0.7.1"
+__version__ = "0.7.2"
import copy
import csv
@@ -126,7 +126,7 @@ class PrettyTable(object):
# Options
self._options = "start end fields header border sortby reversesort sort_key attributes format hrules vrules".split()
self._options.extend("int_format float_format padding_width left_padding_width right_padding_width".split())
- self._options.extend("vertical_char horizontal_char junction_char header_style valign xhtml".split())
+ self._options.extend("vertical_char horizontal_char junction_char header_style valign xhtml print_empty".split())
for option in self._options:
if option in kwargs:
self._validate_option(option, kwargs[option])
@@ -166,6 +166,10 @@ class PrettyTable(object):
self._horizontal_char = kwargs["horizontal_char"] or self._unicode("-")
self._junction_char = kwargs["junction_char"] or self._unicode("+")
+ if kwargs["print_empty"] in (True, False):
+ self._print_empty = kwargs["print_empty"]
+ else:
+ self._print_empty = True
self._format = kwargs["format"] or False
self._xhtml = kwargs["xhtml"] or False
self._attributes = kwargs["attributes"] or {}
@@ -264,7 +268,7 @@ class PrettyTable(object):
self._validate_vrules(option, val)
elif option in ("fields"):
self._validate_all_field_names(option, val)
- elif option in ("header", "border", "reversesort", "xhtml"):
+ elif option in ("header", "border", "reversesort", "xhtml", "print_empty"):
self._validate_true_or_false(option, val)
elif option in ("header_style"):
self._validate_header_style(val)
@@ -697,6 +701,18 @@ class PrettyTable(object):
self._format = val
format = property(_get_format, _set_format)
+ def _get_print_empty(self):
+ """Controls whether or not empty tables produce a header and frame or just an empty string
+
+ Arguments:
+
+ print_empty - True or False"""
+ return self._print_empty
+ def _set_print_empty(self, val):
+ self._validate_option("print_empty", val)
+ self._print_empty = val
+ print_empty = property(_get_print_empty, _set_print_empty)
+
def _get_attributes(self):
"""A dictionary of HTML attribute name/value pairs to be included in the <table> tag when printing HTML
@@ -952,7 +968,8 @@ class PrettyTable(object):
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"""
+ 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 """
options = self._get_options(kwargs)
@@ -960,7 +977,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:
+ if self.rowcount == 0 and (not options["print_empty"] or not options["border"]):
return ""
# Get the rows we need to print, taking into account slicing, sorting, etc.
@@ -998,6 +1015,10 @@ class PrettyTable(object):
bits = [options["junction_char"]]
else:
bits = [options["horizontal_char"]]
+ # For tables with no data or fieldnames
+ if not self._field_names:
+ bits.append(options["junction_char"])
+ return "".join(bits)
for field, width in zip(self._field_names, self._widths):
if options["fields"] and field not in options["fields"]:
continue
@@ -1023,6 +1044,12 @@ class PrettyTable(object):
bits.append(options["vertical_char"])
else:
bits.append(" ")
+ # For tables with no data or field names
+ if not self._field_names:
+ if options["vrules"] in (ALL, FRAME):
+ bits.append(options["vertical_char"])
+ else:
+ bits.append(" ")
for field, width, in zip(self._field_names, self._widths):
if options["fields"] and field not in options["fields"]:
continue
diff --git a/prettytable_test.py b/prettytable_test.py
index 3d0daeb..66a821b 100644
--- a/prettytable_test.py
+++ b/prettytable_test.py
@@ -223,6 +223,25 @@ class HrulesAllBasicTests(BasicTests):
BasicTests.setUp(self)
self.x.hrules = ALL
+class EmptyTableTests(CityDataTest):
+
+ """Make sure the print_empty option works"""
+
+ def setUp(self):
+ CityDataTest.setUp(self)
+ self.y = PrettyTable()
+ self.y.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
+
+ def testPrintEmptyTrue(self):
+ assert self.y.get_string(print_empty=True) != ""
+ assert self.x.get_string(print_empty=True) != self.y.get_string(print_empty=True)
+
+ def testPrintEmptyFalse(self):
+ assert self.y.get_string(print_empty=False) == ""
+ assert self.y.get_string(print_empty=False) != self.x.get_string(print_empty=False)
+
+ def testInteractionWithBorder(self):
+ assert self.y.get_string(border=False, print_empty=True) == ""
class PresetBasicTests(BasicTests):
"""Run the basic tests after using set_style"""