Skip to content

Commit

Permalink
Merge pull request #47 from KingsburyLab/sort
Browse files Browse the repository at this point in the history
Solution: always sort components by amount
  • Loading branch information
rkingsbury authored Oct 17, 2023
2 parents 3a0078a + 6ca9a9e commit ff39bbd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- `Solution.components` is now automatically sorted in descending order of amount, for
consistency with `anions`, `cations`, and `neutrals`.

### Fixed

- Bugfix in `as_dict` to make serialization via `dumpfn` possible. Previously, `Quantity`
Expand Down
12 changes: 4 additions & 8 deletions src/pyEQL/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,26 +495,23 @@ def cations(self) -> dict[str, float]:
Returns the subset of `components` {formula: moles} that are cations. The returned dict is sorted by
amount in descending order.
"""
d = {k: v for k, v in self.components.items() if self.get_property(k, "charge") > 0}
return dict(sorted(d.items(), key=lambda x: x[1], reverse=True))
return {k: v for k, v in self.components.items() if self.get_property(k, "charge") > 0}

@property
def anions(self) -> dict[str, float]:
"""
Returns the subset of `components` {formula: moles} that are anions. The returned dict is sorted by
amount in descending order.
"""
d = {k: v for k, v in self.components.items() if self.get_property(k, "charge") < 0}
return dict(sorted(d.items(), key=lambda x: x[1], reverse=True))
return {k: v for k, v in self.components.items() if self.get_property(k, "charge") < 0}

@property
def neutrals(self) -> dict[str, float]:
"""
Returns the subset of `components` {formula: moles} that are neutral (not charged). The returned dict is sorted by
amount in descending order.
"""
d = {k: v for k, v in self.components.items() if self.get_property(k, "charge") == 0}
return dict(sorted(d.items(), key=lambda x: x[1], reverse=True))
return {k: v for k, v in self.components.items() if self.get_property(k, "charge") == 0}

# TODO - need tests for viscosity
@property
Expand Down Expand Up @@ -1113,8 +1110,7 @@ def get_components_by_element(self) -> dict[str, list]:
d = {}
# by sorting the components according to amount, we ensure that the species
# are sorted in descending order of concentration in the resulting dict
components = dict(sorted(self.components.items(), key=lambda x: x[1], reverse=True))
for s in components:
for s in self.components:
# determine the element and oxidation state
elements = self.get_property(s, "elements")

Expand Down
2 changes: 2 additions & 0 deletions src/pyEQL/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
super().__setitem__(standardize_formula(key), value)
# sort contents anytime an item is set
self.data = dict(sorted(self.items(), key=lambda x: x[1], reverse=True))

def __delitem__(self, key):
super().__delitem__(standardize_formula(key))

0 comments on commit ff39bbd

Please sign in to comment.