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
8 # used for Computer numbers base definitions.
9 ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
12 def makeBase(x, base = len(ALPHA_NUMERIC), table=ALPHA_NUMERIC):
14 Convert from base 10 to any other base.
22 d, m = divmod(x, base)
25 return makeBase(d, base, table) + table[m]
38 # functions that convert Arabic digits to roman numerals
43 3: lambda i,v,x: i+i+i,
47 7: lambda i,v,x: v+i+i,
48 8: lambda i,v,x: v+i+i+i,
55 convert a decimal number in [1,4000) to a roman numeral
70 raise NotImplemented("Value out of roman comprehension")
74 raise NotImplemented("Value Out of Range")
79 i,v = roman_group[base]
81 x,l = roman_group[base]
84 s = roman_value[digit](i,v,x) + s
88 def fromroman(s, rbase = 1):
90 convert a roman numeral (in lowercase) to a decimal integer
103 i, v = roman_group[rbase]
104 x, l = roman_group[rbase*10]
117 for conversion in conversions:
118 if s.endswith(conversion[0]):
119 digit = conversion[1]
120 s = s[:-len(conversion[0])]
126 return digit * rbase + fromroman(s, rbase*10)
129 class simple_multiplier(object):
131 def to_base(self,value,multiplier):
132 return value * (multiplier)
134 def from_base(self,value,multiplier):
138 return value / (multiplier)
141 class simple_inverter(object):
143 def to_base(self,value,multiplier):
147 return (multiplier) / value
149 def from_base(self,value,multiplier):
153 return (multiplier) / value
156 class simple_gain_offset(object):
158 def to_base(self,value,(gain,offset)):
159 return (value * (gain)) + offset
161 def from_base(self,value,(gain,offset)):
165 return (value - offset) / gain
168 class simple_offset_gain(object):
170 def to_base(self,value,(offset,gain)):
171 return (value + offset) * gain
173 def from_base(self,value,(offset,gain)):
177 return (value / gain) - offset
180 class slope_offset(object):
181 ''"convert using points on a graph''"
183 def to_base(self,value,((low_in,high_in),(low_out,high_out))):
184 gain = (high_out-low_out)/(high_in-low_in)
185 offset = low_out - gain*low_in
186 return gain*value+offset
188 def from_base(self,value,((low_out,high_out),(low_in,high_in))):
189 gain = (high_out-low_out)/(high_in-low_in)
190 offset = low_out - gain*low_in
191 return gain*value+offset
194 class double_slope_offset(object):
195 ''"convert using points on a graph, graph split into two slopes''"
197 def to_base(self,value,((low1_in,high1_in),(low1_out,high1_out),(low2_in,high2_in),(low2_out,high2_out))):
198 if low1_in<=value<=high1_in:
199 gain = (high1_out-low1_out)/(high1_in-low1_in)
200 offset = low1_out - gain*low1_in
201 return gain*value+offset
202 if low2_in<=value<=high2_in:
203 gain = (high2_out-low2_out)/(high2_in-low2_in)
204 offset = low2_out - gain*low2_in
205 return gain*value+offset
208 def from_base(self,value,((low1_in,high1_in),(low1_out,high1_out),(low2_in,high2_in),(low2_out,high2_out))):
209 if low1_out<=value<=high1_out:
210 gain = (high1_in-low1_in)/(high1_out-low1_out)
211 offset = low1_in - gain*low1_out
212 return gain*value+offset
213 if low2_out<=value<=high2_out:
214 gain = (high2_in-low2_in)/(high2_out-low2_out)
215 offset = low2_in - gain*low2_out
216 return gain*value+offset
220 class base_converter(object):
222 def to_base(self,value,base):
224 Convert from any base to base 10 (decimal)
226 result = 0L #will contain the long base-10 (decimal) number to be returned
227 position = len(value) #length of the string that is to be converted
229 position = position-1
230 result = long(result + long(long(ALPHA_NUMERIC.find(x))*(long(base)**long(position))))
233 def from_base(self,value,base):
235 Convert from decimal to any base
237 return makeBase(value,base)
240 class roman_numeral(object):
242 def to_base(self,value,junk):
244 Convert from roman numeral to base 10 (decimal)
249 return fromroman(value)
251 def from_base(self,value,junk):
253 Convert from decimal to roman numeral
255 return toroman(value)
259 class function(object):
260 ''"defined simple function can be as complicated as you like, however, both to/from base must be defined.''"
262 #value is assumed to be a string
263 #convert from a defined function to base
264 def to_base(self,value,(to_base,from_base)):
265 exec "y="+to_base[:to_base.find('x')]+str(value)+to_base[to_base.find('x')+1:]
268 def from_base(self,value,(to_base,from_base)):
269 exec "y="+from_base[:from_base.find('x')]+str(value)+from_base[from_base.find('x')+1:]
273 #--------- function definitions from classes ------------
274 m = simple_multiplier()
275 inv = simple_inverter()
276 gof = simple_gain_offset()
277 ofg = simple_offset_gain()
279 dso = double_slope_offset()