Skip to content

Commit

Permalink
Water unit (#41)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sepandhaghighi authored Nov 22, 2024
1 parent 048e680 commit 547660c
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

__ __ _ _ ___ _____ ____ ____ ____ ____
( \/ )( \/ ) / __)( _ )( ___)( ___)( ___)( ___)
Expand Down Expand Up @@ -188,6 +190,12 @@ Info: Chemex method
<td align="center">String</td>
<td align="center"><code>g</code></td>
</tr>
<tr>
<td align="center"><code>--water-unit</code></td>
<td align="center">Water unit</td>
<td align="center">String</td>
<td align="center"><code>g</code></td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -223,6 +231,7 @@ Just fill an issue and describe it. We'll check it ASAP!
<blockquote>19- <a href="https://honestcoffeeguide.com/whole-bean-to-ground-coffee-ratio/">Whole bean to ground coffee calculator</a></blockquote>
<blockquote>20- <a href="https://www.howmany.wiki/wv/">Weight to Volume Converter for Recipes</a></blockquote>
<blockquote>21- <a href="https://chamberlaincoffee.com/blogs/inspiration/how-much-coffee-per-cup-this-is-how-you-get-it-right">How Much Coffee per Cup?</a></blockquote>
<blockquote>22- <a href="https://www.calculator.net/weight-calculator.html">Weight Calculator</a></blockquote>

## Show Your Support
Expand Down
5 changes: 4 additions & 1 deletion mycoffee/__main__.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand Down
46 changes: 44 additions & 2 deletions mycoffee/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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"]):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
11 changes: 10 additions & 1 deletion mycoffee/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Coffee: {2} {7}
Water: {3} g
Water: {3} {8}
Ratio: {4}/{5}
Expand All @@ -32,6 +32,7 @@
"coffee_ratio": 1,
"water_ratio": 1,
"coffee_unit": "g",
"water_unit": "g",
"digits": 3,
"info": ""

Expand Down Expand Up @@ -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}
}
84 changes: 79 additions & 5 deletions test/functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
__ __ _ _ ___ _____ ____ ____ ____ ____
( \/ )( \/ ) / __)( _ )( ___)( ___)( ___)( ___)
Expand All @@ -28,7 +36,7 @@
<BLANKLINE>
Info: V60 method
<BLANKLINE>
>>> 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
Expand All @@ -52,7 +60,31 @@
<BLANKLINE>
Info: Nothing :)
<BLANKLINE>
>>> 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)
__ __ _ _ ___ _____ ____ ____ ____ ____
( \/ )( \/ ) / __)( _ )( ___)( ___)( ___)( ___)
) ( \ / ( (__ )(_)( )__) )__) )__) )__)
(_/\/\_) (__) \___)(_____)(__) (__) (____)(____)
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
Method: `v60`
<BLANKLINE>
Cups: 2
<BLANKLINE>
Coffee: 60 g
<BLANKLINE>
Water: 0.5 kg
<BLANKLINE>
Ratio: 3/50
<BLANKLINE>
Info: Nothing :)
<BLANKLINE>
>>> 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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -119,6 +151,14 @@
8. `oz` - ounce
9. `tbsp` - tablespoon
10. `tsp` - teaspoon
>>> show_water_units_list()
Water units list:
<BLANKLINE>
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
Expand Down Expand Up @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
<BLANKLINE>
1. `g` - gram
2. `kg` - kilogram
3. `lb` - pound
4. `mg` - milligram
5. `oz` - ounce
"""
20 changes: 20 additions & 0 deletions test/verified_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

0 comments on commit 547660c

Please sign in to comment.