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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 | #On the name of ALLAH
#Author :Fouad Teniou
#Date : 08/10/08
#Version : 2.4
""" Class of an equation of a circle of the form Ax^2 + Ay^2 + Dx + Ey + F = 0 (A !=0)
it represents a circle or a point or has no graph , depending of the radius value. And a class
of an equation for the circle of radius r and centred at point (x0,y0). """
import math
class Circle(object):
""" Class that represent an equation of a circle
with A,D,E,F constants properties """
def __init__(self, Avalue,Dvalue,Evalue,Fvalue):
""" Circle construction takes A,D,E,F Constants """
self.__A = float(Avalue)
self.__D = float(Dvalue)
self.__E = float(Evalue)
self.__F = float(Fvalue)
self._b = chr(253)
self._a = self._checkSign(self.__A)
self._d= self._checkSign(self.__D)
self._e = self._checkSign(self.__E)
self._f = self._checkSign(self.__F)
self._g = ((self.__D/self.__A)/2)
self._g1= self.__D/2
self._h =((self.__E/self.__A)/2)
self._h1 = self.__E/2
self._i = self._checkSign(self._g)
self._j = self._checkSign(self._h)
self._k = (-self.__F/self.__A + self._g**2 +self._h**2)
self._k1= (-self.__F + self._g1**2 +self._h1**2)
self._l = "%2.2f" % math.sqrt(abs(self._k))
self._l1 = "%2.2f" % math.sqrt(abs(self._k1))
self._m = "(x%s%s)%s+(y%s%s)%s = %s" % \
(self._i,self._g,self._b,self._j,self._h,self._b,self._k)
self._m1 = "(x%s%s)%s+(y%s%s)%s = %s" % \
(self._i,self._g1,self._b,self._j,self._h1,self._b,self._k1)
self._n = "(%s,%s)" % (-self._g,-self._h)
self._n1 = "(%s,%s)" % (-self._g1,-self._h1)
def __str__(self):
""" String representation of the circle equation,
standard form , centre and radius """
try:
math.sqrt(self._k)
#Circle raises zero degenerate case
assert math.sqrt(self._k) != 0,"The graph is the single point %s" % \
Circle.centre(self)
if self.__A == 0:
return "\n<Equation of a circle : x%s + y%s %s %sx %s %sy %s %s = 0 \
\n\n%s %35s %25s \n\n%s %22s %24s\n" %\
(self._b,self._b,self._d,int(self.D),self._e, \
int(self.E),self._f,int(self.F),
'Standard form','Centre(x0,y0)','Radius r' \
self._m1,Circle.centre(self),Circle.radius(self))
else:
return "\n<Equation of a circle : %sx%s + %sy%s %s %sx %s %sy %s %s = 0 \
\n\n%s %35s %25s \n\n%s %22s %24s\n" %\
(int(self.A)self._b,int(self.A),self._b,self._d,int(self.D),self._e, \
int(self.E),self._f,int(self.F),
'Standard form', 'Centre(x0,y0)','Radius r' \
self._m,Circle.centre(self),Circle.radius(self))
#Circle raises Negative number degenerate case
except ValueError:
raise ValueError,\
" r%s < 0 : Degenerate case has no graph" % self._b
def getA(self):
""" Get method for A attribute """
if self.__A != 0:
return self.__A
else:
raise ValueError,\
" A value should be different than zero "
def setA(self,value):
""" Set method for A attribute """
self.__A = value
def delA(self):
""" Delete method for A attribute """
del self.__A
#Create A property
A = property(getA,setA,delA,"A constant")
def getD(self):
""" Get method for D attribute """
return self.__D
def setD(self,value):
""" Set method for D attribute """
self.__D = value
def delD(self):
""" Delete method for D attribute """
del self.__D
#Create D property
D = property(getD,setD,delD,"D constant")
def getE(self):
""" Get method for E attribute """
return self.__E
def setE(self,value):
""" Set method for E attribute """
self.__E = value
def delE(self):
""" Delete method for E attribute """
del self.__E
#Create E property
E = property(getE,setE,delE,"E constant")
def getF(self):
""" Get method for F attribute """
return self.__F
def setF(self,value):
""" Set method for F attribute """
self.__F = value
def delF(self):
""" Delete method for F attribute """
del self.__F
#Create F property
F = property(getF,setF,delF,"F constant")
def _checkSign(self,value):
""" Utility method to check the values’ signs and return a sign string """
if value >= 0:
return "+"
else:
return ""
def radius(self):
""" Compute radius of a circle """
if self.__A == 1:
return self._l1
else:
return self._l
def centre(self):
""" Compute centre(x0,y0) of a circle """
if self.__A == 1:
return self._n1
else:
return self._n
class Equation(Circle):
"""Class that represent a radius and the centre of a circle """
def __init__(self,x,y,radius):
"""Equation construction takes centre(xValue,yValue
and radius"""
self.__x = float(x)
self.__y = float(y)
self.__radius = float(radius)
self._o = chr(253)
self._p = self.__radius**2
self._q = self._checkSign(-self.__x)
self._r = self._checkSign(-self.__y)
self._s = "(x%s%s)%s + (y%s%s)%s = %s " % \
(self._q,-self.__x,self._o,self._r,-self.__y,self._o,self._p)
self._t = self.__x**2 + self.__y**2 -self._p
self._u = self._checkSign(self._t)
self._v = "x%s + y%s %s %sx %s %sy %s %s = 0 " % \
(self._o,self._o,self._q,-self.__x*2,self._r,-self.__y*2,self._u,self._t)
def __str__(self):
""" String representation of the circle equation, standard form ,centre and radius """
#Equation raises radius value < 0
assert self.__radius > 0, "<Radius value should be greater than zero"
return ( "\n<Equation for the circle of radius (%s)\
centred at (%s,%s) is : \n\n%s < -- > %s" ) % \
(self.__radius,self.__x,self.__y,self._s,self._v)
if __name__ == "__main__":
circle1 = Circle(16,40,16,-7)
print circle1
#Though students might use only values of radius and circle
print radius.circle1()
print centre.circle1()
circle2 = Circle(2,24,0,-81)
print circle2
del circle2.A
circle2.A = 1
print circle2
equation = Equation(2,5,3)
print equation
for doc in (Circle.A,Circle.D,Circle.E,Circle.F):
print doc.__doc__,doc.fget.func_name,doc.fset.func_name,doc.fdel.func_name
|
Comments
All the get/set stuff is not very usefull. Use direct access to class members and add a property only if you want to add specific code to handle the member access (get and/or set).
Thank your comment. However I used the Properties just for that purpose you mentioned, to handle the member access for the Circle equations' constants. It is recommended to use such programming technique for Mathematics equations and scientific programs, for the benefits of the property's set
(original comments were constricted to 3000 characters)
Point 6 is most important.
2) Too much code! Three abbreviated replacement functions (also with _checkSign) ---
5a) _checkSign is independent of the class, so don't make it a method of the class. Thus, another way to write the Circle constructor is with
6) And finally, the property set methods are incorrect. In this example the equation of circle is the same but the standard forms differ. The "set" methods have to recompute all that stuff in the "__init__" method. You could move _g through _n1 computations into a separate method as a function of self, A,D,E and F, which you could then call from __init__, setA, setD, setE, and setF.
finally, read the python library code to get some ideas for how python can work.
I realised that you did run Circle on windows python and this is not for professionals and that is why you thought they were faults in the program, while I can assure you it is running perfectly with no mistakes on DOS. However,I write all my programs for Students and Tutors at the universities and expecting them to be professionals and able to use DOS for windows to run my programs or any other computer language program hint ( The professional way). Regarding the _checkSign Utility method I think your comment does not make sense completely since I learned from a Python professional book how to use Utility methods and the fact that it was the only method that the class inherit, is for others to use the program for other purposes and add methods to suite their needs as may be you are not aware of mathematics of universities level. and python library again is for a non professional people as I realised on your last comment and you could find out yourself my point. 1- start using professional tools ( DOS ) to run the Programs I do write 2- Use professionas resources ( Books ) to learn python 3- Circle is highly superior program only for professional
Sign in to comment