Define a class Circle with method init which initializes a cicle with attribute
radius, having follwing restrictions. a. radius must be numeric value, if not raise
type error with error message "radius must be number".
b. radius must be between 0 to 1000 inclusive on both sides, if not raise the value
error with error message "radius must be between 0 and 1000 inclusive"
c. Define a class method area and circumference which must return values rounded
off to 2 decimals.
Complete the definition of class TestingCircleCreation which tests the behaviour
init method as specification below.
Define the test method of 'test_creating_circle_with_numerical_radius' which
creates circle with radius 2.5 and check if radius matches to value 2.5
Define the test method test_creating_circle_with_negative_radius which checks if
value error exception is raised with the error message "radius must be between 0
and 1000 inclusive", while creating circle of radius 2.5.
Define the test method test_creating_circle_with_greaterthan_radius which checks if
ValueError exception is raised with error message "radius must be between 0 and
1000 inclusive", while creating circle of radius 1000.1.
Define the test method test_creating_circle_with_nonnumeric_radius, which checks if
TypeError exception is raised with error message "radius must be number" while
creating circle of radius 'hello'.
import inspect
import re
import unittest
import math
# Define below the class 'Circle' and it's methods with proper doctests.
class Circle:
def __init__(self, radius):
# Define the initialization method below
[Link]=radius
if not isinstance([Link],(int,float)):
raise TypeError("radius must be a number")
elif([Link]>1000 or [Link]<0):
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
pass
def area(self):
# Define the area functionality below
return [Link]*([Link]**2)
def circumference(self):
return 2*[Link]*[Link]
# Define the circumference functionality below
class TestCircleCreation([Link]):
def test_creating_circle_with_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# the value of [Link] equal to 2.5 or not
c1=Circle(2.5)
[Link]([Link],2.5)
def test_creating_circle_with_negative_radius(self):
# Try Defining a circle 'c' with radius -2.5 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
with [Link](ValueError) as e:
c=Circle(-2.5)
[Link](str([Link]),"radius must be between 0 and 1000
inclusive")
def test_creating_circle_with_greaterthan_radius(self):
# Try Defining a circle 'c' with radius 1000.1 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
with [Link](ValueError) as e:
c=Circle(1000.1)
[Link](str([Link]),"radius must be between 0 and 1000
inclusive")
def test_creating_circle_with_nonnumeric_radius(self):
# Try Defining a circle 'c' with radius 'hello' and see
# if it raises a TypeError with the message
# "radius must be a number"
with [Link](TypeError) as e:
c=Circle("hello")
[Link](str([Link]),"radius must be a number")
********
3 rd
*******
Define a class Circle with method init which initializes a cicle with attribute
radius, having follwing restrictions.
radius must be numeric value, if not raise type error with error message "radius
must be number".
radius must be between 0 to 1000 inclusive on both sides, if not raise the value
error with error message "radius must be between 0 and 1000 inclusive"
Define a class method area and circumference which must return values rounded off
to 2 decimals.
Complete the definition of class TestingCircleCircumference which tests the
behaviour of circumference method as specification below.
Define the test method test_circlecircum_with_random_numerical_radius which creates
circle c1 with radius 2.5 and check if its computed circumference match the value
15.71
Define the test method test_circlecircum_with__min_radius which creates circle c2
with radius 0 and check if its computed circumference match the value 0
Define the test method test_circlecircum_with_max_radius which creates circle c3
with radius 1000 and check if its computed circumference match the value 6283.19
class Circle:
def __init__(self, radius):
# Define the initialization method below
[Link] = radius
if not isinstance([Link],(int,float)):
raise TypeError("radius must be a number")
elif [Link]<0 or [Link]>1000:
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
pass
def area(self):
# Define the area functionality below
return round([Link]*([Link]**2),2)
def circumference(self):
# Define the circumference functionality below
return round(2*[Link]*[Link],2)
class TestCircleCircumference([Link]):
def test_circlecircum_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# it's circumference is 15.71
c1 = Circle(2.5)
[Link]([Link](),15.71)
def test_circlecircum_with_min_radius(self):
# Define a circle 'c2' with radius 0 and check if
# it's circumference is 0.
c2 = Circle(0)
[Link]([Link](),0)
def test_circlecircum_with_max_radius(self):
# Define a circle 'c3' with radius 1000 and check if
# it's circumference is 6283.19.
c3 = Circle(1000)
[Link]([Link](),6283.19)
if __name__ == '__main__':
fptr = open('[Link]', 'w')
runner = [Link](fptr)
[Link](testRunner=runner, exit=False)
[Link]()
with open('[Link]') as fp:
output_lines = [Link]()
pass_count = [ len([Link](r'\.', line)) for line in output_lines if
[Link]('.')
and [Link]('.\n')]
pass_count = pass_count[0]
print(str(pass_count))
doc1 =
[Link](TestCircleCircumference.test_circlecircum_with_random_numeric_rad
ius)
doc2 =
[Link](TestCircleCircumference.test_circlecircum_with_min_radius)
doc3 =
[Link](TestCircleCircumference.test_circlecircum_with_max_radius)
assert1_count = len([Link](r'assertEqual', doc1))
print(str(assert1_count))
assert1_count = len([Link](r'assertEqual', doc2))
print(str(assert1_count))
assert1_count = len([Link](r'assertEqual', doc3))
print(str(assert1_count))
if __name__ == '__main__':
fptr = open('[Link]', 'w')
runner = [Link](fptr)
[Link](testRunner=runner, exit=False)
[Link]()
with open('[Link]') as fp:
output_lines = [Link]()
pass_count = [ len([Link](r'\.', line)) for line in output_lines if
[Link]('.')
and [Link]('.\n')]
pass_count = pass_count[0]
print(str(pass_count))
doc1 =
[Link](TestCircleCreation.test_creating_circle_with_numeric_radius)
doc2 =
[Link](TestCircleCreation.test_creating_circle_with_negative_radius)
doc3 =
[Link](TestCircleCreation.test_creating_circle_with_greaterthan_radius)
doc4 =
[Link](TestCircleCreation.test_creating_circle_with_nonnumeric_radius)
assert1_count = len([Link](r'assertEqual', doc1))
print(str(assert1_count))
assert1_count = len([Link](r'assertEqual', doc2))
assert2_count = len([Link](r'assertRaises', doc2))
print(str(assert1_count), str(assert2_count))
assert1_count = len([Link](r'assertEqual', doc3))
assert2_count = len([Link](r'assertRaises', doc3))
print(str(assert1_count), str(assert2_count))
assert1_count = len([Link](r'assertEqual', doc4))
assert2_count = len([Link](r'assertRaises', doc4))
print(str(assert1_count), str(assert2_count))