Skip to content

Commit

Permalink
Adjustments to composition element deletion and hash by composition
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Forrest committed Sep 23, 2022
1 parent 8363f4a commit 2147a2a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions metallurgy/alloy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ def __setitem__(self, element: str, percentage: float):
else:
if element in self.keys():
super().__delitem__(element)
self.on_change()
self.on_change(change_type="set")

def __delitem__(self, element):
super().__delitem__(element)
self.on_change(change_type="del")

def __init__(
self,
composition: Union[str, dict, Alloy],
constraints: Optional[dict] = None,
constraints: Union[dict, None] = None,
rescale: bool = True,
):

self.composition = parse_composition(composition)

self.constraints = None

if rescale:
Expand All @@ -64,17 +67,14 @@ def __init__(
if rescale:
self.rescale()

else:
self.constraints = None

def __repr__(self):
return self.to_string()

def __eq__(self, other):
return self.to_string() == other.to_string()

def __hash__(self):
return hash(self.to_string())
return hash(self.composition)

@property
def composition(self) -> Composition:
Expand All @@ -87,10 +87,14 @@ def composition(self, value):
value = self.Composition(value, self.on_composition_change)
self._composition = value

def on_composition_change(self):
def on_composition_change(self, change_type=None):
"""Called when composition property changes."""
self.determine_percentage_constraints()

if change_type == "del":
self.clamp_composition()
self.round_composition()

@property
def elements(self) -> list:
"""List of elements in the alloy."""
Expand Down

0 comments on commit 2147a2a

Please sign in to comment.