Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility method to format dictionary #65

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- `format_solutes_dict()` method added into the utils module to help format solutes dictionaries with a unit.

## [0.9.0] - 2023-10-17

### Added
Expand Down
22 changes: 22 additions & 0 deletions src/pyEQL/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ def standardize_formula(formula: str):
return Ion.from_formula(formula).reduced_formula


def format_solutes_dict(solute_dict: dict, units: str):
"""
Formats a dictionary of solutes by converting the amount to a string with the provided units suitable for passing to
use with the Solution class. Note that all solutes must be given in the same units.

Args:
solute_dict: The dictionary to format. This must be of the form dict{str: Number}
e.g. {"Na+": 0.5, "Cl-": 0.9}
units: The units to use for the solute. e.g. "mol/kg"

Returns:
A formatted solute dictionary.

Raises:
TypeError if `solute_dict` is not a dictionary.
"""
if not isinstance(solute_dict, dict):
raise TypeError("solute_dict must be a dictionary. Refer to the doc for proper formatting.")

return {key: f"{value!s} {units}" for key, value in solute_dict.items()}


class FormulaDict(UserDict):
"""
Automatically converts keys on get/set using pymatgen.core.Ion.from_formula(key).reduced_formula.
Expand Down
17 changes: 16 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Tests of pyEQL.utils module

"""
from pytest import raises

from pyEQL.utils import FormulaDict, standardize_formula
from pyEQL.utils import FormulaDict, format_solutes_dict, standardize_formula


def test_standardize_formula():
Expand Down Expand Up @@ -35,3 +36,17 @@ def test_formula_dict():
assert d.get("Na+") == 0.5
d.update({"Br-": 2})
assert d["Br[-]"] == 2


def test_format_solute():
"""
Test formatting solute dictionaries
"""
test = {"Na+": 0.5, "Cl-": 0.5}
base = {"Na+": "0.5 mol/kg", "Cl-": "0.5 mol/kg"}
assert format_solutes_dict(test, units="mol/kg") == base

bad_test = [["Na+", 0.5], ["Cl-", 0.5]]
error_msg = "solute_dict must be a dictionary. Refer to the doc for proper formatting."
with raises(TypeError, match=error_msg):
format_solutes_dict(bad_test, units="mol/kg")
Loading