diff --git a/matrepr/__init__.py b/matrepr/__init__.py index 12e3a57..d8ba96a 100644 --- a/matrepr/__init__.py +++ b/matrepr/__init__.py @@ -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}` """ @@ -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: @@ -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']) @@ -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 diff --git a/matrepr/html_formatter.py b/matrepr/html_formatter.py index d513166..687d344 100644 --- a/matrepr/html_formatter.py +++ b/matrepr/html_formatter.py @@ -21,7 +21,7 @@ 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 @@ -29,7 +29,7 @@ def __init__(self, max_rows, max_cols, num_after_dots, title, indices=False, cel 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 @@ -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 "+" diff --git a/matrepr/latex_formatter.py b/matrepr/latex_formatter.py index f0eb271..54d447b 100644 --- a/matrepr/latex_formatter.py +++ b/matrepr/latex_formatter.py @@ -50,7 +50,7 @@ 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 @@ -58,9 +58,9 @@ def __init__(self, max_rows, max_cols, num_after_dots, title_latex, latex_matrix 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): @@ -68,7 +68,7 @@ def pprint(self, obj): return "" if isinstance(obj, (int, float)): - return self.float_formatter(obj) + return self.floatfmt(obj) if isinstance(obj, complex): r = obj.real @@ -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))) @@ -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)) + "}" diff --git a/tests/test_html.py b/tests/test_html.py index 56de4dd..39eb0ce 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -98,7 +98,7 @@ def test_precision(self): self.assertIn("