Skip to content

Commit 57253a7

Browse files
committed
Refactor: the app.py calculation to be hanlde by a separate module
1 parent d5e3927 commit 57253a7

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed
-148 Bytes
Binary file not shown.
Binary file not shown.

FlaskSimpleCalculator/app.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Flask, render_template, request
2+
from handle_calculation import calculate
23

34
app = Flask(__name__)
45
app.config.from_object(__name__)
@@ -8,24 +9,12 @@
89
def welcome():
910
return render_template('form.html')
1011

11-
1212
@app.route('/result', methods=['POST'])
1313
def result():
1414
operand_1 = request.form.get("operand_1", type=int)
1515
operand_2 = request.form.get("operand_2", type=int)
1616
operator = request.form.get("operator")
17-
if(operand_2 == 0 and operator=='Division'):
18-
result='Invalid_operation'
19-
elif(operator == 'Addition'):
20-
result = operand_1 + operand_2
21-
elif(operator == 'Subtraction'):
22-
result = operand_1 - operand_2
23-
elif(operator == 'Multiplication'):
24-
result = operand_1 * operand_2
25-
elif(operator == 'Division'):
26-
result = operand_1 / operand_2
27-
else:
28-
result = 'Invalid_Choice'
17+
result = calculate(operand_1, operand_2, operator)
2918
entry = result
3019
return render_template('result.html', entry=entry)
3120

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# handle_calculation.py
2+
3+
def calculate(operand_1: int, operand_2: int, operator: str) -> int:
4+
"""
5+
calculation between the operands using the operator and returns a value
6+
7+
Parameters:
8+
9+
-operand_1: the first given value
10+
-operand_2: the second given value
11+
-operator: used to trigger the calculation
12+
"""
13+
if(operand_2 == 0 and operator=='Division'):
14+
result='Invalid_operation'
15+
elif(operator == 'Addition'):
16+
result = operand_1 + operand_2
17+
elif(operator == 'Subtraction'):
18+
result = operand_1 - operand_2
19+
elif(operator == 'Multiplication'):
20+
result = operand_1 * operand_2
21+
elif(operator == 'Division'):
22+
result = operand_1 / operand_2
23+
else:
24+
result = 'Invalid_Choice'
25+
26+
return result

FlaskSimpleCalculator/templates/form.html

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@
2929
</div>
3030
</div>
3131
<div class="form-group">
32-
<label class="control-label col-xs-3">OPERATOR:</label>
33-
<div class="col-xs-5">
34-
<select class="form-control" name="operator">
35-
<option>Addition</option>
36-
<option>Subtraction</option>
37-
<option>Multiplication</option>
38-
<option>Division</option>
39-
</select>
40-
</div>
41-
</div>
32+
<label class="control-label col-xs-3">OPERATOR:</label>
33+
<div class="col-xs-5">
34+
<select class="form-control" name="operator">
35+
<option>Addition</option>
36+
<option>Subtraction</option>
37+
<option>Multiplication</option>
38+
<option>Division</option>
39+
</select>
40+
</div>
41+
</div>
4242
<div class="form-group">
43-
<div class="col-xs-offset-3 col-xs-9">
44-
<input type="submit" class="btn btn-success" value="Submit">
45-
<input type="reset" class="btn btn-danger" value="Reset">
46-
</div>
47-
</div>
48-
</form>
49-
</div>
50-
<div class="col-md-4"></div></div>
43+
<div class="col-xs-offset-3 col-xs-9">
44+
<input type="submit" class="btn btn-success" value="Submit">
45+
<input type="reset" class="btn btn-danger" value="Reset">
46+
</div>
47+
</div>
48+
</form>
49+
</div>
50+
<div class="col-md-4"></div></div>
5151
</div>
5252
</body>
5353
</html>

0 commit comments

Comments
 (0)