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
7 # For the sake of eval'ing some code
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 div, mod = divmod(x, base)
28 return makeBase(div, base, table) + table[mod]
41 # functions that convert Arabic digits to roman numerals
43 0: lambda i, v, x: '',
45 2: lambda i, v, x: i+i,
46 3: lambda i, v, x: i+i+i,
47 4: lambda i, v, x: i+v,
49 6: lambda i, v, x: v+i,
50 7: lambda i, v, x: v+i+i,
51 8: lambda i, v, x: v+i+i+i,
52 9: lambda i, v, x: i+x,
58 convert a decimal number in [1,4000) to a roman numeral
73 raise NotImplementedError("Value out of roman comprehension")
77 raise NotImplementedError("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 # Protection against fractional values
230 value = value.split(".", 1)[0]
232 result = 0L #will contain the long base-10 (decimal) number to be returned
233 position = len(value) #length of the string that is to be converted
235 position = position-1
236 result = long(result + long(long(ALPHA_NUMERIC.find(x))*(long(base)**long(position))))
239 def from_base(self, value, base):
241 Convert from decimal to any base
243 return makeBase(int(value), base)
246 class roman_numeral(object):
248 def to_base(self, value, junk):
250 Convert from roman numeral to base 10 (decimal)
255 return fromroman(value)
257 def from_base(self, value, junk):
259 Convert from decimal to roman numeral
261 return toroman(int(value))
265 class function(object):
266 ''"defined simple function can be as complicated as you like, however, both to/from base must be defined.''"
268 #value is assumed to be a string
269 #convert from a defined function to base
270 def to_base(self, value, (to_base, from_base)):
271 y = 0 # "undefined" y was driving me nuts
272 exec "y="+to_base[:to_base.find('x')]+str(value)+to_base[to_base.find('x')+1:]
275 def from_base(self, value, (to_base, from_base)):
276 y = 0 # "undefined" y was driving me nuts
277 exec "y="+from_base[:from_base.find('x')]+str(value)+from_base[from_base.find('x')+1:]
281 #--------- function definitions from classes ------------
282 m = simple_multiplier()
283 inv = simple_inverter()
284 gof = simple_gain_offset()
285 ofg = simple_offset_gain()
287 dso = double_slope_offset()