Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Obs in f-strings without specifier #190

Merged
merged 5 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pyerrors/obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,12 @@ def __str__(self):
return _format_uncertainty(self.value, self._dvalue)

def __format__(self, format_type):
if format_type == "":
significance = 2
else:
significance = int(float(format_type.replace("+", "").replace("-", "")))
my_str = _format_uncertainty(self.value, self._dvalue,
significance=int(float(format_type.replace("+", "").replace("-", ""))))
significance=significance)
for char in ["+", " "]:
if format_type.startswith(char):
if my_str[0] != "-":
Expand Down
8 changes: 8 additions & 0 deletions tests/obs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,3 +1274,11 @@ def test_format():
assert o1.__format__("+3") == '+0.3480(123)'
assert o1.__format__("+2") == '+0.348(12)'
assert o1.__format__(" 2") == ' 0.348(12)'

def test_f_string_obs():
o1 = pe.pseudo_Obs(0.348, 0.0123, "test")
print(f"{o1}")
print(f"{o1:3}")
print(f"{o1:+3}")
print(f"{o1:-1}")
print(f"{o1: 8}")