From 6ec8531fe5280ff73b61197257a10ac1858399a9 Mon Sep 17 00:00:00 2001 From: LucaVellage Date: Tue, 12 Mar 2024 19:03:29 +0100 Subject: [PATCH] Problem Set 2 Exercise 1 --- .DS_Store | Bin 0 -> 6148 bytes circle.py | 25 +++++++++++++++++++++++++ flask_app.py | 26 +++++++++++++++++++++++++- templates/circle.html | 20 ++++++++++++++++++++ templates/layout.html | 1 + test_circle.py | 24 ++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 circle.py create mode 100644 templates/circle.html create mode 100644 test_circle.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3989e5680693abaedcae7511c063cfbfc60c6a22 GIT binary patch literal 6148 zcmeHKF;2rk5ZnVRGNefdLPBv%gUAm|WL^+K3nVcKQYjj)+YAqTeEF5K#zc4AwC$F`j2%uoV~KV#8~U2CYsn z&&CUx8c+pPf#0Zreer3R26Rp{|NXtb0^#6&F3XdoJIcF=g zL93q6vQ~VaFLOJZ%=PltC^M?c@pM5#)8`j6KefN10D3lCSQpe$6;K6Kfw=p+!n(~IpUGaiyan%QclKR8SA(!8@EF-_Ued7Hl0i?sG};N3it}F z%4vi9|3UTs-!IZLRX`Q^R|=RQ=_GC3lIyJtH^;p;f}ggwg=pbH{sSW Y7x4iwc321^0@FVNRt6nZfge@i4K}Kjq5uE@ literal 0 HcmV?d00001 diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..d327c04 --- /dev/null +++ b/circle.py @@ -0,0 +1,25 @@ +from math import pi + +#Defining Circle class +class Circle: + def __init__(self, radius: float): + self.radius = radius + + #Calculating perimeter of circle + def perimeter(self): + return 2 * pi * self.radius + + #Calculating area of circle + def area(self): + return pi * self.radius ** 2 + + #Defining calculation function based on selected operation + def perform_cir_calc(self, operation: str) -> float: + if operation == 'perimeter': + return f'This circle has a perimeter of {round(self.perimeter(), 2)} cm.' + elif operation == 'area': + return f'This circle has an area of {round(self.area(), 2)} cm.' + +#Converting radius input into float +def convert_radius_to_float(radius: str) -> float: + return float(radius) diff --git a/flask_app.py b/flask_app.py index 8897fa2..1f9afc7 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,11 +1,13 @@ from flask import Flask, render_template, request - from helper import perform_calculation, convert_to_float +from circle import Circle, convert_radius_to_float app = Flask(__name__) # create the instance of the flask class @app.route('/') + + @app.route('/home') def home(): return render_template('home.html') @@ -38,3 +40,25 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/circle', methods=['GET', 'POST']) +def circle(): + if request.method == 'POST': + radius = request.form['radius'] + operation = request.form['operation'] + + try: + radius = convert_radius_to_float(radius=radius) + except ValueError: + return render_template('circle.html', printed_result="Radius must be a number") + + if radius <= 0: + return render_template('circle.html', printed_result="Radius must be a positive number") + + circle_instance = Circle(radius) + result = circle_instance.perform_cir_calc(operation=operation) + + return render_template('circle.html', printed_result=result) + + return render_template('circle.html') diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..2b11445 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,20 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + + + + + +
+ +
+ + {{ printed_result }} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..23b0193 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..97d0382 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,24 @@ + +#Testing both methods in circle.py +import pytest +from math import pi +from circle import Circle + +# Tests for perimeter calculation with positive float input +def test_perimeter(): + + #creating instance for radius = 2 + circle_instance = Circle(radius = 2) + expected_perim_result = f'This circle has a perimeter of {round(2 * pi * 2, 2)} cm.' + actual_perim_result = circle_instance.perform_cir_calc('perimeter') + assert actual_perim_result == expected_perim_result, "Perimeter calculation test failed!" + + +# Tests for area calculation +def test_area(): + + #creating instance for radius = 2 + circle_instance = Circle(radius = 2) + expected_area_result = f'This circle has an area of {round(pi * 2 ** 2, 2)} cm.' + actual_area_result = circle_instance.perform_cir_calc('area') + assert actual_area_result == expected_area_result, "Area calculation test failed!" \ No newline at end of file