Skip to content

Commit

Permalink
Rename float_formatter to floatfmt to match tabulate
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski committed Aug 23, 2023
1 parent 453d763 commit 3dd1cb0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
28 changes: 14 additions & 14 deletions matrepr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ class MatReprParams:

precision: int = 4
"""
Floating-point precision. May be overriden by `float_formatter`.
Floating-point precision. May be overriden by `floatfmt`.
"""

float_formatter: Callable[[float], str] = None
floatfmt: Union[str, Callable[[float], str]] = None
"""
A callable for converting floating point numbers to string.
For convenience may also be a format string.
A format string to format numbers as string, usable by :code:`format()`.
May also be a `Callable` that converts numbers to strings.
If None then `precision` is used.
"""

float_formatter_latex: Callable[[float], str] = None
floatfmt_latex: Callable[[float], str] = None
"""
Overwrites `float_formatter` for LaTeX output. If None then uses `float_formatter` but converts scientific
Overwrites `floatfmt` for LaTeX output. If None then uses `floatfmt` but converts scientific
notation: :raw:`1e22` becomes :raw:`1 \\times 10^{22}`
"""

Expand All @@ -78,7 +78,7 @@ def set_precision(self, precision, g=True):
:param g: Whether to use :code:`g` or :code:`f` formatting.
"""
fmt_str = f".{precision}{'g' if g else ''}"
self.float_formatter = lambda f: format(f, fmt_str)
self.floatfmt = lambda f: format(f, fmt_str)

def _assert_one_of(self, var, choices):
if getattr(self, var) not in choices:
Expand All @@ -97,11 +97,11 @@ def get(self, **kwargs):
setattr(ret, key, value)

# Handy type conversions
if ret.float_formatter is None:
if ret.floatfmt is None:
ret.set_precision(ret.precision)
elif isinstance(ret.float_formatter, str):
fmt_str = ret.float_formatter
ret.float_formatter = lambda f: format(f, fmt_str)
elif isinstance(ret.floatfmt, str):
fmt_str = ret.floatfmt
ret.floatfmt = lambda f: format(f, fmt_str)

# validate
ret._assert_one_of("cell_align", ['center', 'left', 'right'])
Expand All @@ -110,8 +110,8 @@ def get(self, **kwargs):
if ret.title_latex is None:
ret.title_latex = ret.title

if ret.float_formatter_latex is None:
ret.float_formatter_latex = lambda f: python_scientific_to_latex_times10(ret.float_formatter(f))
if ret.floatfmt_latex is None:
ret.floatfmt_latex = lambda f: python_scientific_to_latex_times10(ret.floatfmt(f))

return ret

Expand Down
6 changes: 3 additions & 3 deletions matrepr/html_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@

class HTMLTableFormatter(BaseFormatter):
def __init__(self, max_rows, max_cols, num_after_dots, title, indices=False, cell_align="center",
float_formatter=None, **_):
floatfmt=None, **_):
super().__init__()
self.max_rows = max_rows
self.max_cols = max_cols
self.num_after_dots = num_after_dots
self.title = title
self.indices = indices
self.cell_align = cell_align
self.float_formatter = float_formatter if float_formatter else lambda f: format(f)
self.floatfmt = floatfmt if floatfmt else lambda f: format(f)
self.indent_width = 4
self.left_td_class = None
self.right_td_class = None
Expand All @@ -52,7 +52,7 @@ def pprint(self, obj, current_indent=0):
return ""

if isinstance(obj, (int, float)):
return self.float_formatter(obj)
return self.floatfmt(obj)

if isinstance(obj, complex):
sign = "-" if obj.imag < 0 else "+"
Expand Down
14 changes: 7 additions & 7 deletions matrepr/latex_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ def tex_escape(text):

class LatexFormatter(BaseFormatter):
def __init__(self, max_rows, max_cols, num_after_dots, title_latex, latex_matrix_env,
float_formatter_latex=None, latex_dupe_matrix_env="Bmatrix", **_):
floatfmt_latex=None, latex_dupe_matrix_env="Bmatrix", **_):
super().__init__()
self.max_rows = max_rows
self.max_cols = max_cols
self.num_after_dots = num_after_dots
self.title = title_latex
self.latex_matrix_env = latex_matrix_env
self.dupe_env = latex_dupe_matrix_env
self.float_formatter = float_formatter_latex
if not self.float_formatter:
self.float_formatter = lambda f: python_scientific_to_latex_times10(format(f))
self.floatfmt = floatfmt_latex
if not self.floatfmt:
self.floatfmt = lambda f: python_scientific_to_latex_times10(format(f))
self.indent_width = 4

def pprint(self, obj):
if obj is None:
return ""

if isinstance(obj, (int, float)):
return self.float_formatter(obj)
return self.floatfmt(obj)

if isinstance(obj, complex):
r = obj.real
Expand All @@ -89,7 +89,7 @@ def pprint(self, obj):
if isinstance(obj, DupeList):
fmt = LatexFormatter(max_rows=len(obj), max_cols=1, num_after_dots=0, title_latex=None,
latex_matrix_env=dupe_list_env, latex_dupe_matrix_env=self.dupe_env,
float_formatter_latex=self.float_formatter)
floatfmt_latex=self.floatfmt)
from .adapters.list_like import ListAdapter
return str(fmt.format(ListAdapter(obj)))

Expand All @@ -101,7 +101,7 @@ def pprint(self, obj):
num_after_dots=0, title_latex=None,
latex_matrix_env=self.latex_matrix_env,
latex_dupe_matrix_env=self.dupe_env,
float_formatter_latex=self.float_formatter)
floatfmt_latex=self.floatfmt)
return str(fmt.format(adapter))

return "\\textrm{" + tex_escape(str(obj)) + "}"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_precision(self):
self.assertIn("<td>0.1235</td>", res)

# explicit format string
res = to_html(mat, float_formatter=".2g")
res = to_html(mat, floatfmt=".2g")
self.assertIn("<td>0.12</td>", res)

# explicit precision number
Expand Down
2 changes: 1 addition & 1 deletion tests/test_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_precision(self):
self.assertIn("0.1235", res)

# explicit format string
res = to_latex(mat, float_formatter=".2g")
res = to_latex(mat, floatfmt=".2g")
self.assertIn("0.12", res)

# explicit precision number
Expand Down

0 comments on commit 3dd1cb0

Please sign in to comment.