Skip to content

Commit

Permalink
Adds a more general __eq__ method to AeroSandboxObject, allowing remo…
Browse files Browse the repository at this point in the history
…val of several bespoke __eq__ and __ne__ methods
  • Loading branch information
peterdsharpe committed Mar 29, 2024
1 parent 3da9575 commit f07007f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
5 changes: 0 additions & 5 deletions aerosandbox/atmosphere/thermodynamics/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ def __repr__(self) -> str:

return f"Gas (P = {f(self.pressure, 'Pa')}, T = {self.temperature:.6g} K, ρ = {self.density:.6g} kg/m^3, Pv^gamma = {self.pressure * self.specific_volume ** self.ratio_of_specific_heats: .6g})"

def __eq__(self, other):
if self.__class__ != other.__class__:
return False
return self.__dict__ == other.__dict__

@property
def density(self):
return self.pressure / (self.temperature * self.specific_gas_constant)
Expand Down
25 changes: 25 additions & 0 deletions aerosandbox/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ def __init__(self):
"""
pass

def __eq__(self, other):
"""
Checks if two AeroSandbox objects are value-equivalent. A more sensible default for classes that represent
physical objects than checking for memory equivalence.
This is done by checking if the two objects are of the same type and have the same __dict__.
Args:
other: Another object.
Returns:
bool: True if the objects are equal, False otherwise.
"""
if type(self) != type(other):
return False
if set(self.__dict__.keys()) != set(other.__dict__.keys()):
return False

for key in self.__dict__.keys():
if np.all(self.__dict__[key] == other.__dict__[key]):
continue
else:
return False
return True

def save(self,
filename: Union[str, Path] = None,
verbose: bool = True,
Expand Down
6 changes: 0 additions & 6 deletions aerosandbox/geometry/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ def __init__(self,
def __repr__(self):
return f"Polygon ({self.n_points()} points)"

def __eq__(self, other):
return np.all(self.coordinates == other.coordinates)

def __ne__(self, other):
return not self.__eq__(other)

def x(self) -> np.ndarray:
"""
Returns the x coordinates of the polygon. Equivalent to Polygon.coordinates[:,0].
Expand Down
26 changes: 0 additions & 26 deletions aerosandbox/weights/mass_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,32 +294,6 @@ def __truediv__(self, other: float) -> "MassProperties":
"""
return self.__mul__(1 / other)

def __eq__(self, other: "MassProperties") -> bool:
"""
Returns True if all expected attributes of the two MassProperties objects are exactly equal.
"""
if not isinstance(other, MassProperties):
raise TypeError("MassProperties objects can only be compared to other MassProperties objects.")

return all([
getattr(self, attribute) == getattr(other, attribute)
for attribute in [
"mass",
"x_cg",
"y_cg",
"z_cg",
"Ixx",
"Iyy",
"Izz",
"Ixy",
"Iyz",
"Ixz",
]
])

def __ne__(self, other: "MassProperties") -> bool:
return not self.__eq__(other)

def allclose(self,
other: "MassProperties",
rtol=1e-5,
Expand Down

0 comments on commit f07007f

Please sign in to comment.