8000 Document every public class, method and function (hardening 4) by rafatahmed · Pull Request #122 · rafatahmed/Flahax · GitHub
[go: up one dir, main page]

Skip to content
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
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Python 3.11: inside an f-string, use the other quote type in replacement fields.
A long message is split into adjacent string literals inside brackets; only text that FlahaX
writes out unchanged (report CSS, Markdown tables) keeps its long lines, with `# noqa: E501` on
the closing line of that string.
Every public class, method and function in `src/flahax` has a docstring that says what it
returns or represents; `tests/test_public_docstrings.py` and ruff (D101-D103) check it.
`tests/test_file_headers.py` and the CI `style` job check all of this.

## Required validation
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ quote-style = "preserve"
[tool.ruff.lint]
preview = true
select = ["E", "W", "F"]
# Public classes, methods and functions of the package carry a docstring (pydocstyle D101-D103).
extend-select = ["D101", "D102", "D103"]

[tool.ruff.lint.per-file-ignores]
"!src/**" = ["D101", "D102", "D103"]
1 change: 1 addition & 0 deletions src/flahax/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


def main() -> int:
"""The ``flahax`` console entry point; Ctrl+C prints "Stopped." and exits with 130."""
try:
return _main()
except KeyboardInterrupt:
Expand Down
8 changes: 8 additions & 0 deletions src/flahax/acid_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

@dataclass(frozen=True)
class AcidCandidate:
"""One acid product tried for the pH target: its dose, reagent amount, nutrient contribution
and score, or the error code and message why it was not usable.
"""

product_id: str
formula: str
status: str
Expand All @@ -50,6 +54,10 @@ class AcidCandidate:

@dataclass(frozen=True)
class AcidSelection:
"""Every acid candidate tried and the selected product (None when none fits); the pH must still
be measured after mixing.
"""

candidates: tuple
selected_product_id: str | None
selection_policy: str
Expand Down
4 changes: 4 additions & 0 deletions src/flahax/ammonia.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

@dataclass(frozen=True, slots=True)
class AmmoniaSpeciation:
"""Total ammoniacal nitrogen split into ammonium and dissolved ammonia at a pH and ionic
strength (molal).
"""

total_ammoniacal_nitrogen_molal: float
ph: float
ionic_strength: float
Expand Down 10BC8
9 changes: 9 additions & 0 deletions src/flahax/aqueous_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@

@lru_cache(maxsize=1)
def chemistry():
"""The packaged 25 °C thermodynamic data (``data/chemistry_25c.json``), loaded once."""
return json.loads(files('flahax').joinpath('data/chemistry_25c.json').read_text())


@dataclass(frozen=True)
class AqueousResult:
"""A solved solution: totals, pH, ionic strength, species, activities, saturation indices,
precipitated phases, residuals and charge balance.
"""

totals: dict
ph: float
ionic_strength: float
Expand Down Expand Up @@ -371,6 +376,10 @@ def evaluate(x, jacobian=False):

@dataclass(frozen=True)
class AcidResult:
"""Nitric acid needed to move a solution from its initial pH to the target, with both solved
states; the pH must still be measured after mixing.
"""

initial: AqueousResult
target: AqueousResult
nitric_acid_molal: float
Expand Down
7 changes: 7 additions & 0 deletions src/flahax/batch_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ def acid_record(spec):


def choose_formula(library, formula, crop):
"""The packaged formula name chosen by ``--formula`` (exact) or ``--crop`` (must match one
name); raises DeliveryError otherwise.
"""
names = [f['name'] for f in library['formulas']]
if formula:
if formula not in names:
Expand Down Expand Up @@ -420,6 +423,9 @@ def render(summary):


def build(args):
"""Run the batch for parsed ``flahax batch`` arguments; returns the rendered terminal text and
the summary.
"""
summary = run_batch(
BatchOptions(
water=args.water,
Expand All @@ -441,6 +447,7 @@ def build(args):


def main(argv=None):
"""The ``flahax batch`` command; returns the process exit code."""
parser = argparse.ArgumentParser(
prog='flahax batch',
description=__doc__,
Expand Down
8 changes: 8 additions & 0 deletions src/flahax/competition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@

@dataclass(frozen=True, slots=True)
class CalciumSulfateStruviteCompetition:
"""Gypsum and struvite saturation indices in a mixed calcium, sulfate, magnesium, ammonium and
phosphate solution.
"""

gypsum_si: float
struvite_si: float


@dataclass(frozen=True, slots=True)
class CarbonateCompetition:
"""Calcite, gypsum and struvite saturation indices when carbonate competes for calcium and
magnesium.
"""

calcite_si: float
gypsum_si: float
struvite_si: float
Expand Down
1 change: 1 addition & 0 deletions src/flahax/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,6 @@ def assay_drift(salt: dict) -> str | None:


def is_carbonate(salt: dict) -> bool:
"""True when the salt's chemical formula contains carbonate."""
parsed = parse_formula(str(salt.get("formula") or ""))
return bool(parsed and parsed["carbonate"] > 0)
8 changes: 8 additions & 0 deletions src/flahax/conductivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def ec_recipe_key(stocks, channel='irrigation', *, reagent_doses_g_per_l=None):

@dataclass(frozen=True)
class ECPrediction:
"""An EC estimate in mS/cm from an irrigation profile, with its uncertainty, or the error code
and message why none was made.
"""

status: str
ec_ms_cm: float | None = None
profile_id: str | None = None
Expand All @@ -105,6 +109,10 @@ class ECPrediction:

@dataclass(frozen=True)
class ECDeliveryPlan:
"""Predicted EC, injection volumes and pump commands for an existing recipe, with the audit
record and the limits of an empirical EC estimate.
"""

water_ec_ms_cm: float
predictions: dict
injections: dict
Expand Down
Loading
Loading
0