Skip to content

Commit

Permalink
Ratio type bug (#15)
Browse files Browse the repository at this point in the history
* fix : ratio coefs type changed from int to float

* doc : README.md updated

* feat : is_int function added

* fix : tests updated

* fix : minor edit in tests

* fix : autopep8

* fix : coffee_calc function updated

* doc : CHANGELOG.md updated

* fix : minor bug in tests fixed

* fix : coffee_calc function updated
  • Loading branch information
sepandhaghighi authored Sep 6, 2024
1 parent b07e052 commit 74dafd1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3. Cold brew
4. Cold brew concentrate
5. Moka pot
- `is_int` function
### Changed
- `README.md` updated
- `--coffee-ratio` type changed from `int` to `float`
- `--water-ratio` type changed from `int` to `float`
- `coffee_calc` function updated
## [0.1] - 2024-09-02
### Added
- 6 new methods
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ Info: Chemex method
<tr>
<td align="center"><code>--coffee-ratio</code></td>
<td align="center">Coefficient for the coffee component in the ratio</td>
<td align="center">Integer</td>
<td align="center">Float</td>
<td align="center"><code>1</code></td>
</tr>
<tr>
<td align="center"><code>--water-ratio</code></td>
<td align="center">Coefficient for the water component in the ratio</td>
<td align="center">Integer</td>
<td align="center">Float</td>
<td align="center"><code>17</code></td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions mycoffee/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('--method', help='brewing method', type=str, choices=sorted(METHODS_MAP), default="custom")
parser.add_argument('--info', help='information about the brewing method', type=str)
parser.add_argument('--coffee-ratio', help='coefficient for the coffee component in the ratio', type=int)
parser.add_argument('--water-ratio', help='coefficient for the water component in the ratio', type=int)
parser.add_argument('--coffee-ratio', help='coefficient for the coffee component in the ratio', type=float)
parser.add_argument('--water-ratio', help='coefficient for the water component in the ratio', type=float)
parser.add_argument('--water', help='amount of water in each cup (gr)', type=float)
parser.add_argument('--cups', help='number of cups', type=int)
parser.add_argument('--methods-list', help='brewing methods list', nargs="?", const=1)
Expand Down
24 changes: 20 additions & 4 deletions mycoffee/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
from art import tprint


def is_int(number):
"""
Check that input number is int or not.
:param number: input number
:type number: float or int
:return: result as bool
"""
if int(number) == number:
return True
return False


def print_message(params):
"""
Print message.
Expand Down Expand Up @@ -76,18 +89,21 @@ def load_params(args):
return params


def coffee_calc(params, digit=3):
def coffee_calc(params, digits=3):
"""
Calculate coffee.
:param params: parameters
:type params: dict
:param digit: rounding digit
:type digit: int
:param digits: number of digits up to which the given number is to be rounded
:type digits: int
:return: coffee amount as float
"""
coffee = params["water"] * params["coffee_ratio"] / params["water_ratio"]
return round(coffee, digit)
coffee = round(coffee, digits)
if is_int(coffee):
coffee = int(coffee)
return coffee


def run(args):
Expand Down
14 changes: 11 additions & 3 deletions test/functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@
>>> test_params = {"method":"v60", "cups":2, "coffee":30, "water":335, "coffee_ratio": 3, "water_ratio":50, "info":"V60 method"}
>>> coffee_calc(test_params)
20.1
>>> coffee_calc(test_params, digit=0)
20.0
>>> coffee_calc(test_params, digits=0)
20
>>> is_int(12.1)
False
>>> is_int(12.123)
False
>>> is_int(12.0)
True
>>> is_int(15)
True
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--method', help='brewing method', type=str, choices=sorted(METHODS_MAP), default="custom")
>>> _ = parser.add_argument('--info', help='brewing method info', type=str)
Expand All @@ -93,7 +101,7 @@
<BLANKLINE>
Cups: 1
<BLANKLINE>
Coffee: 15.0 gr
Coffee: 15 gr
<BLANKLINE>
Water: 250 gr
<BLANKLINE>
Expand Down

0 comments on commit 74dafd1

Please sign in to comment.