From 15938ac16ed0050a45e57f104cc8957ada262b08 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 1 May 2019 16:35:34 -0500 Subject: [PATCH 1/4] convergence: pretty_print formatting options --- pytools/convergence.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pytools/convergence.py b/pytools/convergence.py index 61d2182..1018784 100644 --- a/pytools/convergence.py +++ b/pytools/convergence.py @@ -50,7 +50,13 @@ class EOCRecorder(object): def max_error(self): return max(err for absc, err in self.history) - def pretty_print(self, abscissa_label="h", error_label="Error", gliding_mean=2): + def pretty_print(self, + abscissa_label="h", + error_label="Error", + gliding_mean=2, + abscissa_format="{}", + error_format="{}", + eoc_format="{}"): from pytools import Table tbl = Table() @@ -58,14 +64,18 @@ class EOCRecorder(object): gm_eoc = self.estimate_order_of_convergence(gliding_mean) for i, (absc, err) in enumerate(self.history): + absc_str = abscissa_format.format(absc) + err_str = error_format.format(err) if i < gliding_mean-1: - tbl.add_row((str(absc), str(err), "")) + eoc_str = "" else: - tbl.add_row((str(absc), str(err), str(gm_eoc[i-gliding_mean+1, 1]))) + eoc_str = eoc_format.format(gm_eoc[i - gliding_mean + 1, 1]) + + tbl.add_row((absc_str, err_str, eoc_str)) if len(self.history) > 1: - return str(tbl) + "\n\nOverall EOC: %s" \ - % self.estimate_order_of_convergence()[0, 1] + return "{}\n\nOverall EOC: {}".format(tbl, + self.estimate_order_of_convergence()[0, 1]) else: return str(tbl) -- GitLab From cb52fdfa57f6b801bf2bdba15c776a05da692b58 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 1 May 2019 16:35:52 -0500 Subject: [PATCH 2/4] table: add spaces around separator --- pytools/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pytools/__init__.py b/pytools/__init__.py index ded899c..d02a558 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -1447,12 +1447,12 @@ class Table: col_widths = [max(len(row[i]) for row in self.rows) for i in range(columns)] - lines = [ - "|".join([cell.ljust(col_width) - for cell, col_width in zip(row, col_widths)]) + lines = [" | ".join([cell.ljust(col_width) + for cell, col_width in zip(row, col_widths)]) for row in self.rows] - lines[1:1] = ["+".join("-"*col_width - for col_width in col_widths)] + lines[1:1] = ["+".join("-" * (col_width + 1 + (i > 0)) + for i, col_width in enumerate(col_widths))] + return "\n".join(lines) def latex(self, skip_lines=0, hline_after=None): -- GitLab From 06185e7dd5219bf3fa6eac21d618fa895984eb6c Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 1 May 2019 16:36:11 -0500 Subject: [PATCH 3/4] add dumb test for Table and EOCRecorder --- test/test_pytools.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/test_pytools.py b/test/test_pytools.py index 775b68b..bba5dab 100644 --- a/test/test_pytools.py +++ b/test/test_pytools.py @@ -199,6 +199,39 @@ def test_processlogger(): sleep(0.3) +def test_table(): + import math + from pytools import Table + + tbl = Table() + tbl.add_row(("i", "i^2", "i^3", "sqrt(i)")) + + for i in range(8): + tbl.add_row((i, i ** 2, i ** 3, math.sqrt(i))) + + print(tbl) + print() + print(tbl.latex()) + + +def test_eoc(): + from pytools.convergence import EOCRecorder + eoc = EOCRecorder() + + for i in range(1, 8): + eoc.add_data_point(1.0 / i, 10 ** (-i)) + + p = eoc.pretty_print() + print(p) + print() + + p = eoc.pretty_print( + abscissa_format="{:.5e}", + error_format="{:.5e}", + eoc_format="{:>5.2f}") + print(p) + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab From fba7ed40c74bf0194cb71067da2f9f47d63ffd6e Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 1 May 2019 16:52:08 -0500 Subject: [PATCH 4/4] convergence: python2.6 compatibility --- pytools/convergence.py | 14 +++++++------- test/test_pytools.py | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pytools/convergence.py b/pytools/convergence.py index 1018784..d2fa414 100644 --- a/pytools/convergence.py +++ b/pytools/convergence.py @@ -54,9 +54,9 @@ class EOCRecorder(object): abscissa_label="h", error_label="Error", gliding_mean=2, - abscissa_format="{}", - error_format="{}", - eoc_format="{}"): + abscissa_format="%s", + error_format="%s", + eoc_format="%s"): from pytools import Table tbl = Table() @@ -64,17 +64,17 @@ class EOCRecorder(object): gm_eoc = self.estimate_order_of_convergence(gliding_mean) for i, (absc, err) in enumerate(self.history): - absc_str = abscissa_format.format(absc) - err_str = error_format.format(err) + absc_str = abscissa_format % absc + err_str = error_format % err if i < gliding_mean-1: eoc_str = "" else: - eoc_str = eoc_format.format(gm_eoc[i - gliding_mean + 1, 1]) + eoc_str = eoc_format % (gm_eoc[i - gliding_mean + 1, 1]) tbl.add_row((absc_str, err_str, eoc_str)) if len(self.history) > 1: - return "{}\n\nOverall EOC: {}".format(tbl, + return "%s\n\nOverall EOC: %s" % (str(tbl), self.estimate_order_of_convergence()[0, 1]) else: return str(tbl) diff --git a/test/test_pytools.py b/test/test_pytools.py index bba5dab..9783d34 100644 --- a/test/test_pytools.py +++ b/test/test_pytools.py @@ -226,9 +226,9 @@ def test_eoc(): print() p = eoc.pretty_print( - abscissa_format="{:.5e}", - error_format="{:.5e}", - eoc_format="{:>5.2f}") + abscissa_format="%.5e", + error_format="%.5e", + eoc_format="%5.2f") print(p) -- GitLab