From 8736d1cd3cb76a188b78769f7b2bbc38ecc39081 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Fri, 14 Jul 2023 13:38:21 +0100 Subject: [PATCH] feat: CObs format added and complex Corr print improved. (#200) --- pyerrors/correlators.py | 4 +--- pyerrors/obs.py | 8 ++++++++ tests/obs_test.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index 8573cdbf..85e28f4d 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -998,8 +998,6 @@ class Corr: content_string += "Description: " + self.tag + "\n" if self.N != 1: return content_string - if isinstance(self[0], CObs): - return content_string if print_range[1]: print_range[1] += 1 @@ -1010,7 +1008,7 @@ class Corr: else: content_string += str(i + print_range[0]) for element in sub_corr: - content_string += '\t' + ' ' * int(element >= 0) + str(element) + content_string += f"\t{element:+2}" content_string += '\n' return content_string diff --git a/pyerrors/obs.py b/pyerrors/obs.py index 85fdf851..a25dceb8 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -1023,6 +1023,14 @@ class CObs: def __repr__(self): return 'CObs[' + str(self) + ']' + def __format__(self, format_type): + if format_type == "": + significance = 2 + format_type = "2" + else: + significance = int(float(format_type.replace("+", "").replace("-", ""))) + return f"({self.real:{format_type}}{self.imag:+{significance}}j)" + def _format_uncertainty(value, dvalue, significance=2): """Creates a string of a value and its error in paranthesis notation, e.g., 13.02(45)""" diff --git a/tests/obs_test.py b/tests/obs_test.py index ed538943..924d98cd 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -1297,6 +1297,16 @@ def test_f_string_obs(): print(f"{o1:-1}") print(f"{o1: 8}") +def test_f_string_cobs(): + o_real = pe.pseudo_Obs(0.348, 0.0123, "test") + o_imag = pe.pseudo_Obs(0.348, 0.0123, "test") + o1 = pe.CObs(o_real, o_imag) + print(f"{o1}") + print(f"{o1:3}") + print(f"{o1:+3}") + print(f"{o1:-1}") + print(f"{o1: 8}") + def test_compute_drho_fails(): obs = pe.input.json.load_json("tests/data/compute_drho_fails.json.gz") obs.gm()