-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCircle.py
More file actions
66 lines (48 loc) · 1.38 KB
/
Circle.py
File metadata and controls
66 lines (48 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import math
class Circle:
def __init__(self, r=1):
self._radius = self._validate_dimension(r)
def radius(self, r=None):
if r is None:
return self._radius
else:
self._radius = self._validate_dimension(r)
def diameter(self, d=None):
if d is None:
return 2*self.radius()
else:
self.radius(self._validate_dimension(d)/2.0)
def area(self, a=None):
if a is None:
return math.pi * (self.radius() ** 2)
else:
self.radius(math.sqrt(self._validate_dimension(a) / (math.pi)))
def _validate_dimension(self, d):
try:
d = float(d)
except ValueError:
raise ValueError("Circle dimensions must be numeric")
if d < 0.0:
raise ValueError("Circle dimensions must be non-negative")
return d
def __str__(self):
return f"Circle({self._radius:.3})"
def __repr__(self):
return str(self)
### Exception raise and catch examples:
def foo(x):
return bar(x)
def bar(x):
try:
return 2 * baz(x)
except ArithmeticError:
print("The baz call failed, retrying")
return 2 * baz(x + 0.000001)
except TypeError:
pass
def baz(x):
return 1.0 / x
def bat(x):
pass
if __name__ == "__main__":
print("I'm the main module!!")