From 547660ca6138ddc361c27216c4dc881ece1e910f Mon Sep 17 00:00:00 2001 From: Sepand Haghighi Date: Fri, 22 Nov 2024 18:25:40 +0330 Subject: [PATCH] Water unit (#41) * feat : WATER_UNITS_MAP added * feat : --water-unit added * feat : convert_water function added * fix : water_unit added to code * fix : tests updated * fix : tests updated * feat : --water-units-list added * fix : tests updated * fix : tests updated * fix : autopep8 * fix : minor edits * fix : minor edit in tests * fix : verified_test.py updated * doc : CHANGELOG.md updated * doc : README.md updated * fix : minor edit in import section * doc : README.md references section updated * fix : MESSAGE_TEMPLATE updated * fix : tests updated * doc : README.md space bug fixed --- CHANGELOG.md | 6 +++ README.md | 11 +++++- mycoffee/__main__.py | 5 ++- mycoffee/functions.py | 46 ++++++++++++++++++++++- mycoffee/params.py | 11 +++++- test/functions_test.py | 84 +++++++++++++++++++++++++++++++++++++++--- test/verified_test.py | 20 ++++++++++ 7 files changed, 173 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86436b8..9ed1e07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - 1 new coffee unit 1. Cup (`cup`) +- `convert_water` function +- `show_water_units_list` function +- `--water-unit` argument +### Changed +- Test system modified +- `README.md` updated ## [0.7] - 2024-11-21 ### Added - 4 new coffee units diff --git a/README.md b/README.md index 5a7b627..6869ff4 100644 --- a/README.md +++ b/README.md @@ -105,8 +105,10 @@ Info: V60 method ⚠️ You can run `mycoffee --coffee-units-list` to view the supported coffee units +⚠️ You can run `mycoffee --water-units-list` to view the supported water units + ```shell -> mycoffee --method=chemex --water=20 --cups=3 --coffee-ratio=2 --water-ratio=37 --coffee-unit=g +> mycoffee --method=chemex --water=20 --cups=3 --coffee-ratio=2 --water-ratio=37 --coffee-unit=g --water-unit=g __ __ _ _ ___ _____ ____ ____ ____ ____ ( \/ )( \/ ) / __)( _ )( ___)( ___)( ___)( ___) @@ -188,6 +190,12 @@ Info: Chemex method String g + + --water-unit + Water unit + String + g + @@ -223,6 +231,7 @@ Just fill an issue and describe it. We'll check it ASAP!
19- Whole bean to ground coffee calculator
20- Weight to Volume Converter for Recipes
21- How Much Coffee per Cup?
+
22- Weight Calculator
## Show Your Support diff --git a/mycoffee/__main__.py b/mycoffee/__main__.py index abb7ecf..11ed209 100644 --- a/mycoffee/__main__.py +++ b/mycoffee/__main__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- """mycoffee main.""" -from mycoffee.params import METHODS_MAP, COFFEE_UNITS_MAP, EXIT_MESSAGE +from mycoffee.params import METHODS_MAP, EXIT_MESSAGE +from mycoffee.params import COFFEE_UNITS_MAP, WATER_UNITS_MAP from mycoffee.functions import run import argparse @@ -24,7 +25,9 @@ def main(): type=int, default=3) parser.add_argument('--coffee-unit', help='coffee unit', type=str, choices=sorted(COFFEE_UNITS_MAP), default="g") + parser.add_argument('--water-unit', help='water unit', type=str, choices=sorted(WATER_UNITS_MAP), default="g") parser.add_argument('--coffee-units-list', help='coffee units list', nargs="?", const=1) + parser.add_argument('--water-units-list', help='water units list', nargs="?", const=1) parser.add_argument('--methods-list', help='brewing methods list', nargs="?", const=1) parser.add_argument('--version', help='version', nargs="?", const=1) args = parser.parse_args() diff --git a/mycoffee/functions.py b/mycoffee/functions.py index bf2a0f3..c173c13 100644 --- a/mycoffee/functions.py +++ b/mycoffee/functions.py @@ -2,7 +2,8 @@ """mycoffee functions.""" import math from mycoffee.params import MESSAGE_TEMPLATE, METHODS_LIST_TEMPLATE, EMPTY_INFO -from mycoffee.params import MY_COFFEE_VERSION, DEFAULT_PARAMS, METHODS_MAP, COFFEE_UNITS_MAP +from mycoffee.params import MY_COFFEE_VERSION, DEFAULT_PARAMS +from mycoffee.params import METHODS_MAP, COFFEE_UNITS_MAP, WATER_UNITS_MAP from mycoffee.params import RATIO_WARNING_MESSAGE from art import tprint @@ -39,7 +40,8 @@ def print_result(params): params["coffee_ratio"], params["water_ratio"], params["info"], - params["coffee_unit"])) + params["coffee_unit"], + params["water_unit"])) if not check_ratio_limits(params): ratio_lower_limit = METHODS_MAP[method]["ratio_lower_limit"] ratio_upper_limit = METHODS_MAP[method]["ratio_upper_limit"] @@ -93,6 +95,21 @@ def show_coffee_units_list(): COFFEE_UNITS_MAP[unit]['name'])) +def show_water_units_list(): + """ + Show water units list. + + :return: None + """ + print("Water units list:\n") + for i, unit in enumerate(sorted(WATER_UNITS_MAP), 1): + print( + METHODS_LIST_TEMPLATE.format( + i, + unit, + WATER_UNITS_MAP[unit]['name'])) + + def load_params(args): """ Load params. @@ -105,6 +122,8 @@ def load_params(args): for item in params: if getattr(args, item) is not None: params[item] = getattr(args, item) + if getattr(args, "water") is not None: + params["water"] = convert_water(params["water"], params["water_unit"], True) params["method"] = args.method return params @@ -119,6 +138,7 @@ def filter_params(params): """ digits = params["digits"] params["coffee"] = round(params["coffee"], digits) + params["water"] = round(params["water"], digits) if is_int(params["coffee"]): params["coffee"] = int(params["coffee"]) if is_int(params["water_ratio"]): @@ -166,6 +186,25 @@ def convert_coffee(coffee, unit): return coffee +def convert_water(water, unit, reverse=False): + """ + Convert water unit. + + :param water: water amount + :type water: float + :param unit: water unit + :type unit: str + :param reverse: reverse convert flag + :type reverse: bool + :return: converted water amount as float/int + """ + rate = WATER_UNITS_MAP[unit]["rate"] + if reverse: + rate = 1 / rate + water = water * rate + return water + + def calc_coffee(params): """ Calculate coffee. @@ -193,8 +232,11 @@ def run(args): show_methods_list() elif args.coffee_units_list: show_coffee_units_list() + elif args.water_units_list: + show_water_units_list() else: params = load_params(args) params["coffee"] = calc_coffee(params) + params["water"] = convert_water(params["water"], params["water_unit"]) params = filter_params(params) print_result(params) diff --git a/mycoffee/params.py b/mycoffee/params.py index 6cd9161..c28f8d4 100644 --- a/mycoffee/params.py +++ b/mycoffee/params.py @@ -16,7 +16,7 @@ Coffee: {2} {7} -Water: {3} g +Water: {3} {8} Ratio: {4}/{5} @@ -32,6 +32,7 @@ "coffee_ratio": 1, "water_ratio": 1, "coffee_unit": "g", + "water_unit": "g", "digits": 3, "info": "" @@ -202,3 +203,11 @@ "dsp": {"name": "dessertspoon", "rate": 0.27792}, "cup": {"name": "cup", "rate": 0.01158}, } + +WATER_UNITS_MAP = { + "g": {"name": "gram", "rate": 1}, + "oz": {"name": "ounce", "rate": 0.03527396195}, + "lb": {"name": "pound", "rate": 0.00220462262185}, + "mg": {"name": "milligram", "rate": 1000}, + "kg": {"name": "kilogram", "rate": 0.001} +} diff --git a/test/functions_test.py b/test/functions_test.py index c316c68..f65ce89 100644 --- a/test/functions_test.py +++ b/test/functions_test.py @@ -7,7 +7,15 @@ 122 >>> convert_coffee(122, "cb") 921 ->>> test_params = {"method":"v60", "cups":2, "coffee":60, "water":500, "coffee_ratio": 3, "water_ratio":50, "info":"V60 method", "coffee_unit": "g"} +>>> convert_water(1, "g") +1 +>>> convert_water(1, "kg") +0.001 +>>> convert_water(1, "kg", False) +0.001 +>>> convert_water(1, "kg", True) +1000.0 +>>> test_params = {"method":"v60", "cups":2, "coffee":60, "water":500, "coffee_ratio": 3, "water_ratio":50, "info":"V60 method", "coffee_unit": "g", "water_unit": "g"} >>> print_result(test_params) __ __ _ _ ___ _____ ____ ____ ____ ____ ( \/ )( \/ ) / __)( _ )( ___)( ___)( ___)( ___) @@ -28,7 +36,7 @@ Info: V60 method ->>> test_params = {"method":"v60", "cups":2, "coffee":60, "water":500, "coffee_ratio": 3, "water_ratio":50, "info":"", "digits":3, "coffee_unit": "g"} +>>> test_params = {"method":"v60", "cups":2, "coffee":60, "water":500, "coffee_ratio": 3, "water_ratio":50, "info":"", "digits":3, "coffee_unit": "g", "water_unit": "g"} >>> test_params = filter_params(test_params) >>> check_ratio_limits(test_params) == True True @@ -52,7 +60,31 @@ Info: Nothing :) ->>> test_params = {"method":"v60", "cups":2, "coffee":6.0, "water":500, "coffee_ratio": 6, "water_ratio":1000, "info":"", "digits":3, "coffee_unit": "g"} +>>> test_params = {"method":"v60", "cups":2, "coffee":60, "water":0.5, "coffee_ratio": 3, "water_ratio":50, "info":"", "digits":3, "coffee_unit": "g", "water_unit": "kg"} +>>> test_params = filter_params(test_params) +>>> check_ratio_limits(test_params) == True +True +>>> print_result(test_params) + __ __ _ _ ___ _____ ____ ____ ____ ____ +( \/ )( \/ ) / __)( _ )( ___)( ___)( ___)( ___) + ) ( \ / ( (__ )(_)( )__) )__) )__) )__) +(_/\/\_) (__) \___)(_____)(__) (__) (____)(____) + + + +Method: `v60` + +Cups: 2 + +Coffee: 60 g + +Water: 0.5 kg + +Ratio: 3/50 + +Info: Nothing :) + +>>> test_params = {"method":"v60", "cups":2, "coffee":6.0, "water":500, "coffee_ratio": 6, "water_ratio":1000, "info":"", "digits":3, "coffee_unit": "g", "water_unit": "g"} >>> test_params = filter_params(test_params) >>> check_ratio_limits(test_params) == False True @@ -82,7 +114,7 @@ >>> check_ratio_limits(test_params) == True True >>> chemex_params = load_method_params("chemex") ->>> chemex_params == {'info': 'Chemex method', 'water': 240, 'cups': 1, 'coffee_ratio': 1, 'water_ratio': 15, 'digits': 3, 'coffee_unit': 'g'} +>>> chemex_params == {'info': 'Chemex method', 'water': 240, 'cups': 1, 'coffee_ratio': 1, 'water_ratio': 15, 'digits': 3, 'coffee_unit': 'g', 'water_unit': 'g'} True >>> show_methods_list() Methods list: @@ -119,6 +151,14 @@ 8. `oz` - ounce 9. `tbsp` - tablespoon 10. `tsp` - teaspoon +>>> show_water_units_list() +Water units list: + +1. `g` - gram +2. `kg` - kilogram +3. `lb` - pound +4. `mg` - milligram +5. `oz` - ounce >>> test_params = {"method":"v60", "cups":1, "water":335, "coffee_ratio": 3, "water_ratio":50, "info":"V60 method", 'coffee_unit': 'g'} >>> calc_coffee(test_params) 20.1 @@ -146,7 +186,7 @@ >>> test_params["water_ratio"] 50.12345 >>> test_params["water"] -335.12345 +335.12 >>> is_int(12.1) False >>> is_int(12.123) @@ -164,7 +204,9 @@ >>> _ = parser.add_argument('--cups', help='number of cups', type=int) >>> _ = parser.add_argument('--digits', help='number of digits up to which the result is rounded', type=int, default=3) >>> _ = parser.add_argument('--coffee-unit', help='coffee unit', type=str, choices=sorted(COFFEE_UNITS_MAP), default="g") +>>> _ = parser.add_argument('--water-unit', help='water unit', type=str, choices=sorted(WATER_UNITS_MAP), default="g") >>> _ = parser.add_argument('--coffee-units-list', help='coffee units list', nargs="?", const=1) +>>> _ = parser.add_argument('--water-units-list', help='water units list', nargs="?", const=1) >>> _ = parser.add_argument('--methods-list', help='brewing methods list', nargs="?", const=1) >>> _ = parser.add_argument('--version', help='version', nargs="?", const=1) >>> args = parser.parse_args({"--version":True}) @@ -212,6 +254,29 @@ 5000 >>> params["cups"] 1 +>>> args = parser.parse_args(["--method", 'v60', "--water-ratio", '500', "--coffee-ratio", '23', "--water", '5000000', "--water-unit", "mg"]) +>>> params = load_params(args) +>>> params["water"] +5000.0 +>>> params["water_ratio"] +500.0 +>>> params["coffee_ratio"] +23.0 +>>> params["method"] +'v60' +>>> args = parser.parse_args(["--method", 'steep-and-release', "--digits", '1', "--water-unit", "mg"]) +>>> params = load_params(args) +>>> params["coffee"] = calc_coffee(params) +>>> params["water"] +255 +>>> params["coffee"] +15.9375 +>>> params["water_ratio"] +16 +>>> params["coffee_ratio"] +1 +>>> params["method"] +'steep-and-release' >>> args = parser.parse_args(["--method", 'steep-and-release', "--digits", '1']) >>> params = load_params(args) >>> params["coffee"] = calc_coffee(params) @@ -327,4 +392,13 @@ 8. `oz` - ounce 9. `tbsp` - tablespoon 10. `tsp` - teaspoon +>>> args = parser.parse_args(["--water-units-list"]) +>>> run(args) +Water units list: + +1. `g` - gram +2. `kg` - kilogram +3. `lb` - pound +4. `mg` - milligram +5. `oz` - ounce """ diff --git a/test/verified_test.py b/test/verified_test.py index 59af309..bf4c3b9 100644 --- a/test/verified_test.py +++ b/test/verified_test.py @@ -303,4 +303,24 @@ >>> custom_coffee_cup = calc_coffee(custom_params) >>> custom_coffee_cup == 0.16348235294117647 True +>>> convert_water(240, "g") == 240 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "g", True) == 240.0 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "kg") == 0.24 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "kg", True) == 240000.0 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "mg") == 240000 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "mg", True) == 0.24 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "oz") == 8.465750868 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "oz", True) == 6803.885549919067 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "lb") == 0.5291094292440001 # https://www.calculator.net/weight-calculator.html +True +>>> convert_water(240, "lb", True) == 108862.16879993954 # https://www.calculator.net/weight-calculator.html +True """