2 All classes for conversions are defined below:
3 each class should have one method for converting "to_base and another for converting "from_base"
4 the return value is the converted value to or from base
11 # used for Computer numbers base definitions.
12 ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
15 def makeBase(x, base = len(ALPHA_NUMERIC), table=ALPHA_NUMERIC):
17 Convert from base 10 to any other base.
25 d, m = divmod(x, base)
28 return makeBase(d, base, table) + table[m]
41 # functions that convert Arabic digits to roman numerals
46 3: lambda i,v,x: i+i+i,
50 7: lambda i,v,x: v+i+i,
51 8: lambda i,v,x: v+i+i+i,
58 convert a decimal number in [1,4000) to a roman numeral
73 raise NotImplemented("Value out of roman comprehension")
77 raise NotImplemented("Value Out of Range")
82 i,v = roman_group[base]
84 x,l = roman_group[base]
87 s = roman_value[digit](i,v,x) + s
91 def fromroman(s, rbase = 1):
93 convert a roman numeral (in lowercase) to a decimal integer
106 i, v = roman_group[rbase]
107 x, l = roman_group[rbase*10]
120 for conversion in conversions:
121 if s.endswith(conversion[0]):
122 digit = conversion[1]
123 s = s[:-len(conversion[0])]
129 return digit * rbase + fromroman(s, rbase*10)
132 class simple_multiplier(object):
134 def to_base(self,value,multiplier):
135 return value * (multiplier)
137 def from_base(self,value,multiplier):
141 return value / (multiplier)
144 class simple_inverter(object):
146 def to_base(self,value,multiplier):
150 return (multiplier) / value
152 def from_base(self,value,multiplier):
156 return (multiplier) / value
159 class simple_gain_offset(object):
161 def to_base(self,value,(gain,offset)):
162 return (value * (gain)) + offset
164 def from_base(self,value,(gain,offset)):
168 return (value - offset) / gain
171 class simple_offset_gain(object):
173 def to_base(self,value,(offset,gain)):
174 return (value + offset) * gain
176 def from_base(self,value,(offset,gain)):
180 return (value / gain) - offset
183 class slope_offset(object):
184 ''"convert using points on a graph''"
186 def to_base(self,value,((low_in,high_in),(low_out,high_out))):
187 gain = (high_out-low_out)/(high_in-low_in)
188 offset = low_out - gain*low_in
189 return gain*value+offset
191 def from_base(self,value,((low_out,high_out),(low_in,high_in))):
192 gain = (high_out-low_out)/(high_in-low_in)
193 offset = low_out - gain*low_in
194 return gain*value+offset
197 class double_slope_offset(object):
198 ''"convert using points on a graph, graph split into two slopes''"
200 def to_base(self,value,((low1_in,high1_in),(low1_out,high1_out),(low2_in,high2_in),(low2_out,high2_out))):
201 if low1_in<=value<=high1_in:
202 gain = (high1_out-low1_out)/(high1_in-low1_in)
203 offset = low1_out - gain*low1_in
204 return gain*value+offset
205 if low2_in<=value<=high2_in:
206 gain = (high2_out-low2_out)/(high2_in-low2_in)
207 offset = low2_out - gain*low2_in
208 return gain*value+offset
211 def from_base(self,value,((low1_in,high1_in),(low1_out,high1_out),(low2_in,high2_in),(low2_out,high2_out))):
212 if low1_out<=value<=high1_out:
213 gain = (high1_in-low1_in)/(high1_out-low1_out)
214 offset = low1_in - gain*low1_out
215 return gain*value+offset
216 if low2_out<=value<=high2_out:
217 gain = (high2_in-low2_in)/(high2_out-low2_out)
218 offset = low2_in - gain*low2_out
219 return gain*value+offset
223 class base_converter(object):
225 def to_base(self,value,base):
227 Convert from any base to base 10 (decimal)
229 result = 0L #will contain the long base-10 (decimal) number to be returned
230 position = len(value) #length of the string that is to be converted
232 position = position-1
233 result = long(result + long(long(string.find(ALPHA_NUMERIC,x))*(long(base)**long(position))))
236 def from_base(self,value,base):
238 Convert from decimal to any base
240 return makeBase(value,base)
243 class roman_numeral(object):
245 def to_base(self,value,junk):
247 Convert from roman numeral to base 10 (decimal)
252 return fromroman(value)
254 def from_base(self,value,junk):
256 Convert from decimal to roman numeral
258 return toroman(value)
262 class function(object):
263 ''"defined simple function can be as complicated as you like, however, both to/from base must be defined.''"
265 #value is assumed to be a string
266 #convert from a defined function to base
267 def to_base(self,value,(to_base,from_base)):
268 exec "y="+to_base[:string.find(to_base,'x')]+str(value)+to_base[string.find(to_base,'x')+1:]
271 def from_base(self,value,(to_base,from_base)):
272 exec "y="+from_base[:string.find(from_base,'x')]+str(value)+from_base[string.find(from_base,'x')+1:]
276 #--------- function definitions from classes ------------
277 m = simple_multiplier()
278 inv = simple_inverter()
279 gof = simple_gain_offset()
280 ofg = simple_offset_gain()
282 dso = double_slope_offset()