From 2b0aeec8728f89013523cbe35b6c8db5adc075be Mon Sep 17 00:00:00 2001 From: Simon Billinge Date: Thu, 4 Jul 2024 04:13:26 -0400 Subject: [PATCH] simplifying error message handling in diffraction objects. add .idea to gitigbore --- .gitignore | 3 ++ .../scattering_objects/diffraction_objects.py | 45 ++++++------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 313f3b88..f49476b4 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ setup.cfg # Rever rever/ + +# PyCharm +.idea/ diff --git a/src/diffpy/utils/scattering_objects/diffraction_objects.py b/src/diffpy/utils/scattering_objects/diffraction_objects.py index e809e018..bd5b16db 100644 --- a/src/diffpy/utils/scattering_objects/diffraction_objects.py +++ b/src/diffpy/utils/scattering_objects/diffraction_objects.py @@ -11,6 +11,11 @@ XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES XUNITS = ["degrees", "radians", "rad", "deg", "inv_angs", "inv_nm", "nm-1", "A-1"] +x_grid_emsg = ( + "objects are not on the same x-grid. You may add them using the self.add method " + "and specifying how to handle the mismatch." +) + class Diffraction_object: def __init__(self, name="", wavelength=None): @@ -57,10 +62,7 @@ def __add__(self, other): elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to sum two Diffraction_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may add them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: summed.on_tth[1] = self.on_tth[1] + other.on_tth[1] summed.on_q[1] = self.on_q[1] + other.on_q[1] @@ -74,10 +76,7 @@ def __radd__(self, other): elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to sum two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may add them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: summed.on_tth[1] = self.on_tth[1] + other.on_tth[1] summed.on_q[1] = self.on_q[1] + other.on_q[1] @@ -91,10 +90,7 @@ def __sub__(self, other): elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to subtract two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may subtract them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: subtracted.on_tth[1] = self.on_tth[1] - other.on_tth[1] subtracted.on_q[1] = self.on_q[1] - other.on_q[1] @@ -108,10 +104,7 @@ def __rsub__(self, other): elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to subtract two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may subtract them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: subtracted.on_tth[1] = other.on_tth[1] - self.on_tth[1] subtracted.on_q[1] = other.on_q[1] - self.on_q[1] @@ -125,10 +118,7 @@ def __mul__(self, other): elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to multiply two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may multiply them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: multiplied.on_tth[1] = self.on_tth[1] * other.on_tth[1] multiplied.on_q[1] = self.on_q[1] * other.on_q[1] @@ -140,10 +130,7 @@ def __rmul__(self, other): multiplied.on_tth[1] = other * self.on_tth[1] multiplied.on_q[1] = other * self.on_q[1] elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may multiply them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: multiplied.on_tth[1] = self.on_tth[1] * other.on_tth[1] multiplied.on_q[1] = self.on_q[1] * other.on_q[1] @@ -157,10 +144,7 @@ def __truediv__(self, other): elif not isinstance(other, Diffraction_object): raise TypeError("I only know how to multiply two Scattering_object objects") elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "objects are not on the same x-grid. You may multiply them using the self.add method and" - "specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: divided.on_tth[1] = self.on_tth[1] / other.on_tth[1] divided.on_q[1] = self.on_q[1] / other.on_q[1] @@ -172,10 +156,7 @@ def __rtruediv__(self, other): divided.on_tth[1] = other / self.on_tth[1] divided.on_q[1] = other / self.on_q[1] elif self.on_tth[0].all() != other.on_tth[0].all(): - raise RuntimeError( - "Diffraction objects are not on the same x-grid. You may multiply them using the self.add" - "method and specifying how to handle the mismatch." - ) + raise RuntimeError(x_grid_emsg) else: divided.on_tth[1] = other.on_tth[1] / self.on_tth[1] divided.on_q[1] = other.on_q[1] / self.on_q[1]