From 0b4c32c8849ebe28157c22c0e294a8007af78aed Mon Sep 17 00:00:00 2001 From: prusnibandela Date: Sun, 17 Mar 2024 22:22:24 +0100 Subject: [PATCH] Task 1 commit --- circle.py | 36 ++++++++++++++++++++++++++++++++ flask_app.py | 31 +++++++++++++++++++++++++++ templates/circle_calculator.html | 24 +++++++++++++++++++++ templates/layout.html | 1 + test_circle.py | 21 +++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 circle.py create mode 100644 templates/circle_calculator.html create mode 100644 test_circle.py diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..12e5996 --- /dev/null +++ b/circle.py @@ -0,0 +1,36 @@ +class Circle: + """ + A class to represent a circle and perform calculations to it. + Attributes: + radius(float): The radius of the circle + """ + + def __init__(self, radius: float): + """ + Constructs the necessary attributes for the circle class. + Parameters: + radius(float): The radius of the circle. + """ + if radius < 0: + raise ValueError("positive radius expected") + self.radius = radius + + def area(self) -> float: + """ + Calculates the area of the circle. + Returns: + float: The area of the circle. + """ + assert self.radius >= 0, "positive radius expected" + pi = 3.14159265359 + return round(pi * (self.radius ** 2), 2) + + def perimeter(self) -> float: + """ + Calculates the perimeter of the circle. + Returns: + float: The perimeter of the circle. + """ + assert self.radius >= 0, "positive radius expected" + pi = 3.14159265359 + return round(2 * pi * self.radius, 2) \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..cb4fa0f 100644 --- a/flask_app.py +++ b/flask_app.py @@ -2,6 +2,8 @@ from helper import perform_calculation, convert_to_float +from circle import Circle + app = Flask(__name__) # create the instance of the flask class @@ -38,3 +40,32 @@ 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_calculations(): + if request.method == 'POST': + radius = request.form['radius'] + calculation = request.form['calculation'] + + if calculation not in ['area', 'perimeter']: + return render_template('circle_calculator.html', result="Calculation must be either 'area' or 'perimeter'.") + + try: + radius = float(radius) + except ValueError: + return render_template('circle_calculator.html', result="Radius must be a valid number.") + + circle = Circle(radius) + + if calculation == 'area': + result = circle.area() + else: + result = circle.perimeter() + + return render_template('circle_calculator.html', result=f"The {calculation} of the circle with radius {radius} is {result}.") + return render_template('circle_calculator.html') + + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/templates/circle_calculator.html b/templates/circle_calculator.html new file mode 100644 index 0000000..d776993 --- /dev/null +++ b/templates/circle_calculator.html @@ -0,0 +1,24 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + + + + +
+ +
+ + {% if result %} +

Result: {{ result }}

+ {% else %} +

Please enter a radius and select a calculation to see the result.

+ {% endif %} +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..43e21c5 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..af3b258 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,21 @@ +from circle import Circle + + +def test_area(): + radius = 5 + manual_area = round(3.14159265359 * (radius ** 2), 2) + circle = Circle(radius) + assert round(circle.area(), 2) == manual_area, "Area test failed" + + +def test_perimeter(): + radius = 5 + manual_perimeter = round(2 * 3.14159265359 * radius, 2) + circle = Circle(radius) + assert round(circle.perimeter(), 2) == manual_perimeter, "Perimeter test failed" + + +if __name__ == "__main__": + test_area() + test_perimeter() + print("All tests successfully passed.") \ No newline at end of file