Skip to content

Mode #130

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

Merged
merged 13 commits into from
Jun 8, 2025
Merged

Mode #130

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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
mycoffee --version
mycoffee --method=chemex --water=20 --cups=3 --coffee-ratio=2 --water-ratio=37 --message="Temp: 92 C"
mycoffee --method=chemex --water=20 --cups=3 --coffee-ratio=2 --water-ratio=37 --message="Temp: 92 C" --ignore-warnings
mycoffee --method=chemex --coffee=2 --cups=3 --coffee-ratio=2 --water-ratio=37 --message="Temp: 92 C" --mode="coffee-to-water"
- name: Install dev-requirements
run: |
python otherfiles/requirements-splitter.py
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1. Clever dripper
2. Phin filter
- `--coffee` argument
- `--mode` argument
### Changed
- Test system modified
- `README.md` updated
Expand Down
3 changes: 2 additions & 1 deletion mycoffee/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""mycoffee main."""
from mycoffee.params import METHODS_MAP, EXIT_MESSAGE, FILE_FORMATS_LIST
from mycoffee.params import METHODS_MAP, EXIT_MESSAGE, FILE_FORMATS_LIST, MODES_LIST
from mycoffee.params import COFFEE_UNITS_MAP, WATER_UNITS_MAP, TEMPERATURE_UNITS_MAP
from mycoffee.functions import run, validate_positive_int, validate_positive_float
import argparse
Expand Down Expand Up @@ -54,6 +54,7 @@ def main() -> None:
parser.add_argument('--version', help='version', nargs="?", const=1)
parser.add_argument('--info', help='info', nargs="?", const=1)
parser.add_argument('--ignore-warnings', help='ignore warnings', nargs="?", const=1)
parser.add_argument('--mode', help='conversion mode', type=str.lower, choices=MODES_LIST, default="water-to-coffee")
parser.add_argument('--save-path', help='file path to save', type=str)
parser.add_argument('--save-format', help='file format', type=str.lower, choices=FILE_FORMATS_LIST, default="text")
args = parser.parse_args()
Expand Down
92 changes: 86 additions & 6 deletions mycoffee/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
message=params["message"],
grind=params["grind"],
temperature=params["temperature"],
strength=params["strength"])
strength=params["strength"],
mode=params["mode"])
return result


Expand Down Expand Up @@ -320,6 +321,8 @@
params[item] = getattr(args, item)
if getattr(args, "water") is None:
params["water"] = convert_water(params["water"], params["water_unit"])
if getattr(args, "coffee") is None:
params["coffee"] = convert_coffee(params["coffee"], params["coffee_unit"])
if getattr(args, "temperature") is None:
params["temperature"] = convert_temperature(
params["temperature"],
Expand Down Expand Up @@ -441,14 +444,18 @@
return True


def convert_coffee(coffee: float, unit: str) -> Union[float, int]:
def convert_coffee(coffee: float, unit: str, reverse: bool = False) -> Union[float, int]:
"""
Convert and return the coffee amount as a float or int.

:param coffee: coffee amount
:param unit: coffee unit
:param reverse: reverse convert flag
"""
coffee = coffee * COFFEE_UNITS_MAP[unit]["rate"]
rate = COFFEE_UNITS_MAP[unit]["rate"]
if reverse:
rate = 1 / rate
coffee = coffee * rate
if unit == "cb":
coffee = math.ceil(coffee)
return coffee
Expand Down Expand Up @@ -484,10 +491,25 @@
return coffee


def get_result(params: Dict[str, Union[str, int, float]],
enable_filter: bool = True) -> Dict[str, Union[str, int, float, dict]]:
def calc_water(ratio: float, coffee: float, water_unit: str, coffee_unit: str) -> float:
"""
Get result.
Calculate water.

:param ratio: coffee/water ratio
:param coffee: coffee amount
:param water_unit: water unit
:param coffee_unit: coffee unit
"""
coffee_gram = convert_coffee(coffee, coffee_unit, True)
water_gram = coffee_gram * (1 / ratio)
water = convert_water(water_gram, water_unit)
return water


def get_result_by_water(params: Dict[str, Union[str, int, float]],
enable_filter: bool = True) -> Dict[str, Union[str, int, float, dict]]:
"""
Get result by water.

:param params: parameters
:param enable_filter: filter flag
Expand Down Expand Up @@ -527,6 +549,64 @@
return result_params


def get_result_by_coffee(params: Dict[str, Union[str, int, float]],
enable_filter: bool = True) -> Dict[str, Union[str, int, float, dict]]:
"""
Get result by coffee.

:param params: parameters
:param enable_filter: filter flag
"""
result_params = params.copy()
result_params["ratio"] = params["coffee_ratio"] / params["water_ratio"]
result_params["coffee"] = {

Check warning on line 562 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L560-L562

Added lines #L560 - L562 were not covered by tests
"total": result_params["cups"] * params["coffee"],
"cup": params["coffee"],
"ratio": params["coffee_ratio"],
"unit": params["coffee_unit"]}
result_params["water"] = {

Check warning on line 567 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L567

Added line #L567 was not covered by tests
"total": None,
"cup": calc_water(
ratio=result_params["ratio"],
coffee=params["coffee"],
water_unit=params["water_unit"],
coffee_unit=params["coffee_unit"]),
"ratio": params["water_ratio"],
"unit": params["water_unit"]}
result_params["grind"] = {

Check warning on line 576 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L576

Added line #L576 was not covered by tests
"unit": "um",
"value": params["grind"],
"type": get_grind_type(params["grind"]),
}
result_params["temperature"] = {

Check warning on line 581 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L581

Added line #L581 was not covered by tests
"unit": params["temperature_unit"],
"value": params["temperature"]
}
for item in ["temperature_unit", "water_ratio", "coffee_ratio", "coffee_unit", "water_unit"]:
del result_params[item]
result_params["water"]["total"] = result_params["cups"] * result_params["water"]["cup"]
result_params["strength"] = get_brew_strength(ratio=result_params["ratio"])

Check warning on line 588 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L586-L588

Added lines #L586 - L588 were not covered by tests
if enable_filter:
result_params = filter_params(result_params)
result_params["warnings"] = get_warnings(result_params)
return result_params

Check warning on line 592 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L590-L592

Added lines #L590 - L592 were not covered by tests


def get_result(params: Dict[str, Union[str, int, float]],
enable_filter: bool = True) -> Dict[str, Union[str, int, float, dict]]:
"""
Get result.

:param params: parameters
:param enable_filter: filter flag
"""
if params["mode"] == "water-to-coffee":
result_params = get_result_by_water(params=params, enable_filter=enable_filter)
else:
result_params = get_result_by_coffee(params=params, enable_filter=enable_filter)

Check warning on line 606 in mycoffee/functions.py

View check run for this annotation

Codecov / codecov/patch

mycoffee/functions.py#L606

Added line #L606 was not covered by tests
return result_params


def run(args: argparse.Namespace) -> None:
"""
Run program.
Expand Down
5 changes: 5 additions & 0 deletions mycoffee/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

Temperature: {temperature[value]} {temperature[unit]}

Mode: {mode}

Message: {message}
"""

Expand All @@ -62,6 +64,7 @@
"water_unit": "g",
"temperature_unit": "C",
"digits": 3,
"mode": "water-to-coffee",
"message": ""

}
Expand Down Expand Up @@ -427,3 +430,5 @@
}

FILE_FORMATS_LIST = ["text", "json"]

MODES_LIST = ["water-to-coffee", "coffee-to-water"]
Loading