ActiveState Code

Recipe 573457: Converter


Converter can be used to convert fundamental measures, metric, mass, liquid and temperature. The output is set to give the best conversion accuracy and layout that could be used for professional projects

Python
  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#On the name of ALLAH
#Author: Fouad Teniou
#Date : 17/06/08
#version :2.4

import re            #Get re class
import math          #Get math class

class Converter:
   def __init__(self,other,unit=0):     # Initialize
      self.unit = unit
      self.other = other
   def __mul__(self):
	return self.unit * self.other
   def __div__(self):
	return round((self.other/self.unit),2)
   def __repr__(self):
	return "\n <Converter: Operation of %s, to convert the value of (%s)>" % (self.__class__.__name__,(self.other)) #class name and the value

class FeetMetre(Converter):              # Inherits
   def __init__(self,other):
      Converter.__init__(self,other,0.3048) #Run Converter init
   def measure(self):
     print '\n\t\t\t',"Feet --> Metre"
     m = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.2f" % self.other))
     L = round(((int(m.group(1)))*( self.unit))+(((float(m.group(2))/12))*( self.unit)),3)
     M = re.match(r"(?P<int>\d+)\.(\d*)", str(L))
     print '\n\t\t\t',m.group(1)+"ft",m.group(2)+"in" +" = "+ M.group(1)+"m",M.group(2)[:2]+"cm"

class MetreFeet(FeetMetre):
   def __init__(self,other):
      FeetMetre.__init__(self,other) #Run FeetMetre init
   def measure(self):
     print '\n\t\t\t',"Metre --> Feet"
     n = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.2f" % self.other))
     o = re.match(r"(?P<int>\d+)\.(\d*)", str(Converter.__div__(self)))
     print '\n\t\t\t',n.group(1)+"m",n.group(2)+"cm" +" = "+ o.group(1)+"ft","%2.2s" % (str(round((float(o.group(2))*(0.12)),0)))+"in"

class MileKilometre(Converter):
   def __init__(self,other):
      Converter.__init__(self,other,1.609)     #Run Converter init
   def measure(self):
     print '\n\t\t\t',"Mile --> KiloMetre"
     print '\n\t\t\t',str(self.other)+'ml'+' = '+"%2.2f" % float(Converter.__mul__(self))+'Km'

class KilometreMile(MileKilometre):
   def __init__(self,other):
      MileKilometre.__init__(self,other) #Run MileKilometre init
   def measure(self):
     print '\n\t\t\t',"Kilometre --> Mile"
     print '\n\t\t\t',(str(se1f.other))+'Km'+' = ',+ str(Converter.__div__(self))+'ml'

class YardMetre(Converter):
   def __init__(self,other):
      Converter.__init__(se1f,other,0.9144) #Run Converter init
   def measure(self):
     print '\n\t\t\t',"Yard --> Metre"
     m = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.2f" % (Converter.__mul__(self)))) 
     print '\n\t\t\t',str(self.other)+'yd'+' = '+ m.group(1)+'m' ,"%2.2s" % (m.group(2))+'cm'

class MetreYard(YardMetre):
   def __init__(self,other):
      YardMetre.__init__(self,other)
   def measure(self):
     print '\n\t\t\t',"Metre --> Yard"
     m = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.2f" % self. other))
     print '\n\t\t\t',m.group(1)+'m',m.group(2)+'cm'+' = '+ str(Converter.__div__(self))+'yd'


class PoundKilo(Converter):
   def __init__(self,other):
      Converter.__init__(self,other,0.4536) #Run Converter init
   def measure(self):
     print '\n\t\t\t',"Pound --> Kilogram"
     m = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.2f" % self.other))
     L = "%2.3f" % (round(((int(m.group(1)))*(self.unit))+(((float(m.group(2))/16))*(self.unit)),3))
     M = re.match(r"(?P<int>\d+)\.(\d*)", str(L))
     print '\n\t\t\t',m.group(1)+"lb",m.group(2)+"oz" +" = "+ M.group(1)+"Kg",M.group(2)+"g"

class KiloPound(PoundKilo):               # Run PoundKilo init
   def __init__(self,other):
      PoundKilo.__init__(self,other)
   def measure(self):
     print '\n\t\t\t',"Kilogram --> Pound"
     n = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.3f" % self.other))
     o = re.match(r"(?P<int>\d+)\.(\d*)", str(Converter.__div__(self)))
     print '\n\t\t\t',n.group(1)+"Kg",n.group(2)+"g" +" = "+ o.group(1)+"lb","%2.2s" % (str(round((float(o.group(2))*(0.16)),0)))+"oz"

class GallonLitre(Converter):
   def __init__(self,other):
      Converter.__init__(self,other,4.55) #Run Converter init
   def measure(self):
     print '\n\t\t\t',"British and American Gallon --> Litre"
     p = re.match(r"(?P<int>\d+)\.(\d*)", str(float(Converter.__mul__(self))))
     print '\n\t\t\t',str(self.other)+'(BR)gal'+' = '+ ("%2.2f" % float(self.other*1.2))+'(AM)gal'+' = '+p.group(1)+'l' , "%2.2s" % (p.group(2))+'cl'

class LitreGallon(GallonLitre):
   def __init__(self,other):
      GallonLitre.__init__(self,other)     # Run GallonLitre init
   def measure(self):
     print '\n\t\t\t',"Litre --> British and American Gallon"
     q = re.match(r"(?P<int>\d+)\.(\d*)", str("%2.2f" % self.other))
     print '\n\t\t\t',q.group(1)+"l",q.group(2)+"cl" +" = " ,"%2.2f" % float (Converter.__div__(self))+'(BR)gal'+' = ', "%2.2f" % float((Converter.__div__(self))*1.2)+'(AM)gal'

class TemperatureD(Converter):     
   def __init__(self,other):
      Converter.__init__(self,other,1.8)   #Run Converter init
   def measure(self):
     print '\n\t\t\t',"Degree Celsius --> Fahrenheit"
     print '\n\t\t\t',str(self.other)+chr(248)+'C'+'='+str("%2.1f" % (32+float(Converter.__mul__(self))))+chr(248)+'F'

class TemperatureF(TemperatureD):
   def __init__(self,other):
      TemperatureD.__init__(self,other) #Run TemperatureD init
   def measure(self):
     print '\n\t\t\t', "Fahrenheit --> Degree Celsius"
     B = self.other-32
     self.other = B
     Print '\n\t\t\t',str(self.other+32)+chr(248)+'F'+'='+str("%2.1f" % (float(Converter.__div__(self))))+chr(248)+'C'
 
if __name__=="__main__": 
   a=MetreFeet(2.0)
   print a
   a.measure() 
   b=FeetMetre(6.11)
   print b
   b.measure()
   c = MileKilometre(1) 
   print c
   c.measure()
   d = KilometreMile(1) 
   print d
   d.measure()
   e = YardMetre(3)
   print e
   e.measure()
   f= MetreYard(3.8)
   print f
   f.measure()
   g = PoundKilo(7.9)
   print g
   g.measure()
   h = KiloPound(5.8)
   print h
   h.measure()
   k = GallonLitre(2.22) 
   print k
   k.measure()
   l = LitreGallon(5)
   printl
   l.measure()
   m = TemperatureD(0) 
   print m
   m.measure()
   n = TemperatureF(3.8) 
   print n
   n.measure()

---------------------------------------------------------------------------
PS: if you run Converter on Dos make sure that you use your own path address 

c: \hp\bin \Python>python "C: \hp\bin \Python \Scripts\Myscripts\Converter5. py"
 
<Converter: Operation of Metre Feet, to convert the value of (2.0)>
 
            Metre --> Feet
 
            2m OOcm = 6ft 7.in
 
<Converter: Operation of Feet Metre, to convert the value of(6.11)>
 
            Feet --> Metre
 
            6ft 11 in = 2m 1Ocm
 
<Converter: Operation of MileKilometre, to convert the value of (1)>
 
            Mile --> KiloMetre
 
            1ml = 1.61Km
 
<Converter: Operation of Kilo metre Mile, to convert the value of (1)>
 
            Kilometre --> Mile
 
            1Km = 0.62ml
 
<Converter: Operation of YardMetre, to convert the value of (3)>
 
            Yard --> Metre
 
            3yd = 2m 74cm
 
<Converter: Operation of Metre Yard, to convert the value of (3 .8)>
 
            Metre --> Yard
 
            3m 80cm = 4.16yd
 
<Converter: Operation of PoundKilo, to convert the value of (7.9)>
 
            Pound --> Kilogram
 
            7lb 90z = 3Kg 430g
 
<Converter: Operation of KiloPound, to convert the value of (5.8)>
 
            Kilogram --> Pound
 
            5Kg 800g = 12lb 130z
 
<Converter: Operation ofGallonLitre, to convert the value of(2.22)>
 
            British and American Gallon --> Litre
 
            2.22(BR)gal = 2.66(AM)gal = 10l lOcl
 
<Converter: Operation of LitreGallon, to convert the value of (5)>

            Litre --> British and American Gallon
 
            5l OOcl = 1.10(BR)gal = 1.32(AM)gal
 
<Converter: Operation of TemperatureD, to convert the value of (0)>
 
            Degree Celsius --> Fahrenheit
 
            O°C = 32.0°F
 
<Converter: Operation ofTemperatureF, to convert the value of (3.8)>
 
            Fahrenheit --> Degree Celsius
 
            3.8°F = -15.7°C
 
c:\hp\bin\Python>
 
-----------------------------------------------------------------------
Alternatively if you run Converter using Pythonwin you can import each class individually and use the type of conversion you want as follows:

>>> from Converter import TemperatureD
>>> for i  in range (1,10):
	a = TemperatureD(i)
	a
	a.measure( ) 

<Converter: Operation of TemperatureD, to convert the value of (0)>
 
            Degree Celsius --> Fahrenheit
 
            O°C = 32.0°F
<Converter: Operation of TemperatureD, to convert the value of (1)>
 
            Degree Celsius --> Fahrenheit
 
            1°C = 33.8°F
<Converter: Operation of TemperatureD, to convert the value of (2)>
 
            Degree Celsius --> Fahrenheit
 
            2°C = 35.6°F
and so on ......................................................

>>> for i  in (32,33.8,35.6):
	b = TemperatureF(i)
	b
	b.measure()

<Converter: Operation of TemperatureF, to convert the value of (32.0)>
 
            Fahrenheit --> Degree Celsius 
 
            32.0°F = O°C
<Converter: Operation of TemperatureF, to convert the value of (33.8)>
 
            Fahrenheit --> Degree Celsius 
 
            33.8°F = 1°C
<Converter: Operation of TemperatureF, to convert the value of (35.6)>
 
            Fahrenheit --> Degree Celsius 
 
            35.6°F = 2°C



--------------------------------------------------------------------------
>>> from Converter import FeetMetre
>>> a = FeetMetre(6,11)
>>> a.measure()
            Feet --> Metre
 
            6ft 11 in = 2m 1Ocm

>>> b = MetreFeet(2,10)
>>> b.measure()
            Metre --> Feet
   
            2m 1Ocm  = 6ft 11 in  
PS.
Make sure that you save Converter in your python Lib to be able to import it or while your Converter file is open on Pythonwin 

Sign in to comment