Skip to content

Commit

Permalink
Change balance attribute to is_balanced method
Browse files Browse the repository at this point in the history
  • Loading branch information
sheodun authored Dec 30, 2023
1 parent 5b73635 commit a2b2645
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Version numbers use calendar versioning based on `YY.MM.MICRO`. See the [CalVer](https://calver.org) website for more information about this versioning convention. The format of this changelog follows the approach outlined on the [Keep a Changelog](https://keepachangelog.com) website.

## 23.12

#### Changed
- Changed the `balance` attribute of ChemicalEquation to use a method instead `is_balanced`
- Refactored tests and documentation to mention `is_balanced` instead of `balance`

## 23.11

#### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This example uses the `ChemicalEquation` class to get properties of the reactant
import chemics as cm

ce = cm.ChemicalEquation('2 HCl + 2 Na -> 2 NaCl + H2')
ce.balance
ce.is_balanced()
# Returns True for balanced equation

ce.rct_properties
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/chemical_equation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The ``ChemicalEquation`` class provides product and reactant properties from an
>>> eq = cm.ChemicalEquation('2 HCl + 2 Na -> 2 NaCl + H2')

# Check if atomic elements of the reactants and products are balanced
>>> eq.balance
>>> eq.is_balanced()
True

# Total number of atomic elements for each product
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This example uses the ``ChemicalEquation`` class to get properties of the reacta
import chemics as cm
ce = cm.ChemicalEquation('2 HCl + 2 Na -> 2 NaCl + H2')
ce.balance
ce.is_balanced()
# The returns True for balanced equation
ce.rct_properties
Expand Down
15 changes: 6 additions & 9 deletions src/chemics/chemical_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ class ChemicalEquation:
Total number of moles on product side of the equation.
prod_mass : float
Total mass of the products.
balance : bool
Balance between atomic elements in reactants and products.
Examples
--------
>>> ce = cm.ChemicalEquation('2 HCl + 2 Na -> 2 NaCl + H2')
>>> ce.balance
>>> ce.is_balanced()
True
>>> ce = cm.ChemicalEquation('2 HCl + 2 Na -> 2 NaCl + H2')
Expand Down Expand Up @@ -83,7 +81,6 @@ def __init__(self, eq, names=None):
self._parse_equation()
self._assign_rct_attrs()
self._assign_prod_attrs()
self._check_balance()

def _parse_equation(self):
"""
Expand Down Expand Up @@ -194,17 +191,17 @@ def _assign_prod_attrs(self):
self.prod_moles = sum_moles
self.prod_mass = sum_masses

def _check_balance(self):
def is_balanced(self) -> bool:
"""
Check balance of atomic elements in reactants or products.
"""
tol = 1e-4
self.balance = True

if self.rct_elements == self.prod_elements:
return
return True

for x, y in zip(self.rct_elements.values(), self.prod_elements.values()):
if abs(x - y) > tol:
self.balance = False
break
return False

return True
4 changes: 2 additions & 2 deletions tests/test_chemical_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_eq1():
eq = '2 HCl + 2 Na -> 2 NaCl + H2'
ce = cm.ChemicalEquation(eq)
assert ce.names is None
assert ce.balance is True
assert ce.is_balanced() is True
assert ce.rct_properties['HCl']['moles'] == approx(2)
assert ce.rct_properties['HCl']['species'] == 'HCl'
assert ce.rct_properties['HCl']['molwt'] == approx(36.458)
Expand All @@ -29,7 +29,7 @@ def test_eq2():
eq = '5 NaCl + 3 H2O -> CH4 + 2.2 CHAR + CH2OHCHO + GCO2'
names = {'CHAR': 'C', 'GCO2': 'CO2'}
ce = cm.ChemicalEquation(eq, names)
assert ce.balance is False
assert ce.is_balanced() is False
assert ce.prod_properties['GCO2']['moles'] == approx(1)
assert ce.prod_properties['GCO2']['species'] == 'CO2'
assert ce.prod_properties['GCO2']['molwt'] == approx(44.009)
Expand Down

0 comments on commit a2b2645

Please sign in to comment.