-
Notifications
You must be signed in to change notification settings - Fork 0
rtyata01/mero.python
Folders and files
| Name | Name | Last commit message | Last commit date |
|---|---|---|---|
Repository files navigation
"""
This module provides basic examples of Python conventions and best practices.
Includes:
- Function definitions with appropriate naming and docstrings.
- Class definitions using CamelCase.
- Proper imports, whitespace, and error handling.
- A basic test using unittest.
"""
import math
import unittest
# Constants
PI = 3.14159
class CircleShape:
"""
Class to represent a circle with a given radius.
"""
def __init__(self, radius):
"""
Initialize the CircleShape instance with a radius.
Parameters:
radius (float): The radius of the circle.
"""
self.radius = radius
def area(self):
"""
Calculate the area of the circle.
Returns:
float: The area of the circle.
"""
return PI * self.radius ** 2
def calculate_area(radius):
"""
Calculate the area of a circle with the given radius.
Parameters:
radius (float): The radius of the circle.
Returns:
float: The area of the circle.
"""
return PI * radius ** 2
def print_results(results):
"""
Print a list of results, each on a new line.
Parameters:
results (list): The list of results to print.
"""
for result in results:
print(result)
def main():
"""
Main function to demonstrate usage of the CircleShape class and functions.
"""
radius = 5.0
circle = CircleShape(radius)
area = calculate_area(radius)
print(f"The area of the circle with radius {radius} is {area}.")
print("Results from CircleShape class:")
print_results([circle.area()])
# Error Handling Example
def divide_numbers(a, b):
"""
Divide two numbers with error handling for division by zero.
Parameters:
a (float): The numerator.
b (float): The denominator.
Returns:
float: The result of the division.
"""
try:
# Attempt to convert inputs to integers
num1 = int(a)
num2 = int(b)
# Attempt to divide num1 by num2
result = num1 / num2
return result
except ValueError as ve:
# Handle case where conversion to integer fails
print(f"ValueError: Invalid input. Please provide valid integers. Details: {ve}")
return None
except ZeroDivisionError as zde:
# Handle case where division by zero occurs
print(f"ZeroDivisionError: Cannot divide by zero. Details: {zde}")
return None
except Exception as e:
# Handle any other unexpected errors
print(f"Unexpected error occurred: {e}")
return None
# Unit Tests
class TestCircleShape(unittest.TestCase):
def test_area(self):
"Test the area calculation."
circle = CircleShape(3)
self.assertAlmostEqual(circle.area(), PI * 3 ** 2)
def test_divide_numbers(self):
"Test division with error handling."
self.assertEqual(divide_numbers(10, 2), 5)
self.assertIsNone(divide_numbers(10, 0))
self.assertIsNone(divide_numbers("ten", 2))
if __name__ == "__main__":
main()
unittest.main()About
Mock samples and features in python
Resources
Security policy
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published