Skip to content

Commit

Permalink
limit oxi state float precision in Species __str|repr__ to .2f
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Nov 10, 2023
1 parent 77a92bd commit d95e2ad
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pymatgen/core/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,12 +996,13 @@ def ionic_radius(self) -> float | None:
if self._oxi_state:
dct = self._el.data
oxi_str = str(int(self._oxi_state))
if oxi_str in dct.get("Ionic radii hs", {}):
warnings.warn(f"No default ionic radius for {self}. Using hs data.")
return dct["Ionic radii hs"][oxi_str]
if oxi_str in dct.get("Ionic radii ls", {}):
warnings.warn(f"No default ionic radius for {self}. Using ls data.")
return dct["Ionic radii ls"][oxi_str]
warn_msg = f"No default ionic radius for {self}."
if ion_rad := dct.get("Ionic radii hs", {}).get(oxi_str):
warnings.warn(f"{warn_msg} Using hs data.")
return ion_rad
if ion_rad := dct.get("Ionic radii ls", {}).get(oxi_str):
warnings.warn(f"{warn_msg} Using ls data.")
return ion_rad
warnings.warn(f"No ionic radius for {self}!")
return None

Expand Down Expand Up @@ -1062,7 +1063,10 @@ def __repr__(self):
def __str__(self):
output = self.symbol
if self.oxi_state is not None:
output += f"{formula_double_format(abs(self.oxi_state))}{'+' if self.oxi_state >= 0 else '-'}"
abs_charge = formula_double_format(abs(self.oxi_state))
if isinstance(abs_charge, float):
abs_charge = f"{abs_charge:.2f}"
output += f"{abs_charge}{'+' if self.oxi_state >= 0 else '-'}"
if self._spin is not None:
spin = self._spin
output += f",{spin=}"
Expand All @@ -1072,7 +1076,10 @@ def to_pretty_string(self) -> str:
"""String without properties."""
output = self.symbol
if self.oxi_state is not None:
output += f"{formula_double_format(abs(self.oxi_state))}{'+' if self.oxi_state >= 0 else '-'}"
abs_charge = formula_double_format(abs(self.oxi_state))
if isinstance(abs_charge, float):
abs_charge = f"{abs_charge:.2f}"
output += f"{abs_charge}{'+' if self.oxi_state >= 0 else '-'}"
return output

def get_nmr_quadrupole_moment(self, isotope: str | None = None) -> float:
Expand Down

0 comments on commit d95e2ad

Please sign in to comment.