From 2fe5706f7e8abfa3a0381da4e3343f2e67d9c256 Mon Sep 17 00:00:00 2001 From: "luke@maurits.id.au" Date: Sun, 1 Jul 2012 21:05:57 +0000 Subject: Switched int_format and float_format to use fancy new Python string formatting. This allows e.g. binary, hex, etc. formatting of integers, scientific notation formatting of floats and other fancy stuff that wasn't possible previously. git-svn-id: http://prettytable.googlecode.com/svn/trunk@76 0f58610c-415a-11de-9c03-5d6cfad8e937 --- prettytable.py | 20 ++++++++++---------- prettytable_test.py | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/prettytable.py b/prettytable.py index c1d0f9d..27a548c 100644 --- a/prettytable.py +++ b/prettytable.py @@ -212,10 +212,10 @@ class PrettyTable(object): self._validate_true_or_false(option, val) elif option in ("header_style"): self._validate_header_style(val) - elif option in ("int_format"): - self._validate_int_format(option, val) - elif option in ("float_format"): - self._validate_float_format(option, val) +# elif option in ("int_format"): +# self._validate_int_format(option, val) +# elif option in ("float_format"): +# self._validate_float_format(option, val) elif option in ("vertical_char", "horizontal_char", "junction_char"): self._validate_single_char(option, val) elif option in ("attributes"): @@ -482,7 +482,7 @@ class PrettyTable(object): int_format - integer format string""" return self._int_format def _set_int_format(self, val): - self._validate_option("int_format", val) +# self._validate_option("int_format", val) for field in self._field_names: self._int_format[field] = val int_format = property(_get_int_format, _set_int_format) @@ -494,7 +494,7 @@ class PrettyTable(object): float_format - floating point format string""" return self._float_format def _set_float_format(self, val): - self._validate_option("float_format", val) +# self._validate_option("float_format", val) for field in self._field_names: self._float_format[field] = val float_format = property(_get_float_format, _set_float_format) @@ -749,9 +749,9 @@ class PrettyTable(object): def _format_value(self, field, value): if isinstance(value, int) and field in self._int_format: - value = _unicode(("%%%sd" % self._int_format[field]) % value) + value = _unicode(("{0:" + self._int_format[field] + "}").format(value)) elif isinstance(value, float) and field in self._float_format: - value = _unicode(("%%%sf" % self._float_format[field]) % value) + value = _unicode(("{0:" + self._float_format[field] + "}").format(value)) return _unicode(value) def _compute_widths(self, rows, options): @@ -1135,8 +1135,8 @@ def main(): x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"]) x.sortby = "Population" x.reversesort = True - x.int_format["Area"] = "04" - x.float_format = "6.1" + x.int_format["Area"] = "04d" + x.float_format = "6.1f" x.align["City name"] = "l" # Left align city names x.add_row(["Adelaide", 1295, 1158259, 600.5]) x.add_row(["Brisbane", 5905, 1857594, 1146.4]) diff --git a/prettytable_test.py b/prettytable_test.py index a3adc4d..9af2bc8 100644 --- a/prettytable_test.py +++ b/prettytable_test.py @@ -297,7 +297,7 @@ class FloatFormatBasicTests(BasicTests): def setUp(self): BasicTests.setUp(self) - self.x.float_format = "6.2" + self.x.float_format = "6.2f" class FloatFormatTests(unittest.TestCase): @@ -308,12 +308,12 @@ class FloatFormatTests(unittest.TestCase): self.x.add_row(["sqrt(2)", sqrt(2)]) def testNoDecimals(self): - self.x.float_format = ".0" + self.x.float_format = ".0f" self.x.caching = False assert "." not in self.x.get_string() def testRoundTo5DP(self): - self.x.float_format = ".5" + self.x.float_format = ".5f" string = self.x.get_string() assert "3.14159" in string assert "3.141592" not in string @@ -324,7 +324,7 @@ class FloatFormatTests(unittest.TestCase): assert "1.414213" not in string def testPadWith2Zeroes(self): - self.x.float_format = "06.2" + self.x.float_format = "06.2f" string = self.x.get_string() assert "003.14" in string assert "002.72" in string -- cgit v1.2.1