X-Git-Url: http://git.maemo.org/git/?p=gonvert;a=blobdiff_plain;f=src%2Fconverters.py;h=219df1de017e0311475f37a7d2962b02e4cee5d6;hp=f7604193e2b644bf2a2bc10bd0514c5ce641d1a2;hb=989d15b85b8a64aa7b213f165b99001159ae6c1c;hpb=dea52fff5581e3d217397c3e50c1ef2c9213ac5f diff --git a/src/converters.py b/src/converters.py index f760419..219df1d 100644 --- a/src/converters.py +++ b/src/converters.py @@ -4,8 +4,8 @@ All classes for conversions are defined below: the return value is the converted value to or from base """ - -import evil_globals +# For the sake of eval'ing some code +import math # used for Computer numbers base definitions. @@ -22,34 +22,34 @@ def makeBase(x, base = len(ALPHA_NUMERIC), table=ALPHA_NUMERIC): >> makeBase(11, 16) 'b' """ - d, m = divmod(x, base) - if not d: - return table[m] - return makeBase(d, base, table) + table[m] + div, mod = divmod(x, base) + if not div: + return table[mod] + return makeBase(div, base, table) + table[mod] # roman numerals roman_group = { - 1: ('i','v'), - 10: ('x','l'), - 100: ('c','d'), - 1000: ('m','A'), - 10000: ('B','C'), + 1: ('i', 'v'), + 10: ('x', 'l'), + 100: ('c', 'd'), + 1000: ('m', 'A'), + 10000: ('B', 'C'), } # functions that convert Arabic digits to roman numerals roman_value = { - 0: lambda i,v,x: '', - 1: lambda i,v,x: i, - 2: lambda i,v,x: i+i, - 3: lambda i,v,x: i+i+i, - 4: lambda i,v,x: i+v, - 5: lambda i,v,x: v, - 6: lambda i,v,x: v+i, - 7: lambda i,v,x: v+i+i, - 8: lambda i,v,x: v+i+i+i, - 9: lambda i,v,x: i+x, + 0: lambda i, v, x: '', + 1: lambda i, v, x: i, + 2: lambda i, v, x: i+i, + 3: lambda i, v, x: i+i+i, + 4: lambda i, v, x: i+v, + 5: lambda i, v, x: v, + 6: lambda i, v, x: v+i, + 7: lambda i, v, x: v+i+i, + 8: lambda i, v, x: v+i+i+i, + 9: lambda i, v, x: i+x, } @@ -70,21 +70,21 @@ def toroman(n): 'xv' """ if n < 0: - raise NotImplemented("Value out of roman comprehension") + raise NotImplementedError("Value out of roman comprehension") elif n == 0: - '' + pass elif n >= 4000: - raise NotImplemented("Value Out of Range") + raise NotImplementedError("Value Out of Range") base = 1 s = '' while n > 0: - i,v = roman_group[base] - base = base * 10 - x,l = roman_group[base] - digit = n % 10 - n = (n-digit)/10 - s = roman_value[digit](i,v,x) + s + i, v = roman_group[base] + base = base * 10 + x, l = roman_group[base] + digit = n % 10 + n = (n-digit)/10 + s = roman_value[digit](i, v, x) + s return s @@ -99,9 +99,9 @@ def fromroman(s, rbase = 1): 15 """ if len(s) == 0: - return 0 + return 0 elif rbase > 1000: - return 0 + return 0 i, v = roman_group[rbase] x, l = roman_group[rbase*10] @@ -131,10 +131,10 @@ def fromroman(s, rbase = 1): class simple_multiplier(object): - def to_base(self,value,multiplier): + def to_base(self, value, multiplier): return value * (multiplier) - def from_base(self,value,multiplier): + def from_base(self, value, multiplier): if multiplier == 0: return 0.0 else: @@ -143,13 +143,13 @@ class simple_multiplier(object): class simple_inverter(object): - def to_base(self,value,multiplier): + def to_base(self, value, multiplier): if value == 0: return 0.0 else: return (multiplier) / value - def from_base(self,value,multiplier): + def from_base(self, value, multiplier): if value == 0: return 0.0 else: @@ -158,10 +158,10 @@ class simple_inverter(object): class simple_gain_offset(object): - def to_base(self,value,(gain,offset)): + def to_base(self, value, (gain, offset)): return (value * (gain)) + offset - def from_base(self,value,(gain,offset)): + def from_base(self, value, (gain, offset)): if gain == 0: return 0.0 else: @@ -170,10 +170,10 @@ class simple_gain_offset(object): class simple_offset_gain(object): - def to_base(self,value,(offset,gain)): + def to_base(self, value, (offset, gain)): return (value + offset) * gain - def from_base(self,value,(offset,gain)): + def from_base(self, value, (offset, gain)): if gain == 0: return 0.0 else: @@ -183,12 +183,12 @@ class simple_offset_gain(object): class slope_offset(object): ''"convert using points on a graph''" - def to_base(self,value,((low_in,high_in),(low_out,high_out))): + def to_base(self, value, ((low_in, high_in), (low_out, high_out))): gain = (high_out-low_out)/(high_in-low_in) offset = low_out - gain*low_in return gain*value+offset - def from_base(self,value,((low_out,high_out),(low_in,high_in))): + def from_base(self, value, ((low_out, high_out), (low_in, high_in))): gain = (high_out-low_out)/(high_in-low_in) offset = low_out - gain*low_in return gain*value+offset @@ -197,23 +197,23 @@ class slope_offset(object): class double_slope_offset(object): ''"convert using points on a graph, graph split into two slopes''" - def to_base(self,value,((low1_in,high1_in),(low1_out,high1_out),(low2_in,high2_in),(low2_out,high2_out))): - if low1_in<=value<=high1_in: + def to_base(self, value, ((low1_in, high1_in), (low1_out, high1_out), (low2_in, high2_in), (low2_out, high2_out))): + if low1_in <= value <= high1_in: gain = (high1_out-low1_out)/(high1_in-low1_in) offset = low1_out - gain*low1_in return gain*value+offset - if low2_in<=value<=high2_in: + if low2_in <= value <= high2_in: gain = (high2_out-low2_out)/(high2_in-low2_in) offset = low2_out - gain*low2_in return gain*value+offset return 0.0 - def from_base(self,value,((low1_in,high1_in),(low1_out,high1_out),(low2_in,high2_in),(low2_out,high2_out))): - if low1_out<=value<=high1_out: + def from_base(self, value, ((low1_in, high1_in), (low1_out, high1_out), (low2_in, high2_in), (low2_out, high2_out))): + if low1_out <= value <= high1_out: gain = (high1_in-low1_in)/(high1_out-low1_out) offset = low1_in - gain*low1_out return gain*value+offset - if low2_out<=value<=high2_out: + if low2_out <= value <= high2_out: gain = (high2_in-low2_in)/(high2_out-low2_out) offset = low2_in - gain*low2_out return gain*value+offset @@ -222,40 +222,43 @@ class double_slope_offset(object): class base_converter(object): - def to_base(self,value,base): + def to_base(self, value, base): """ Convert from any base to base 10 (decimal) """ + # Protection against fractional values + value = value.split(".", 1)[0] + result = 0L #will contain the long base-10 (decimal) number to be returned position = len(value) #length of the string that is to be converted for x in value: position = position-1 - result = long(result + long(long(string.find(ALPHA_NUMERIC,x))*(long(base)**long(position)))) + result = long(result + long(long(ALPHA_NUMERIC.find(x))*(long(base)**long(position)))) return result - def from_base(self,value,base): + def from_base(self, value, base): """ Convert from decimal to any base """ - return makeBase(value,base) + return makeBase(int(value), base) class roman_numeral(object): - def to_base(self,value,junk): + def to_base(self, value, junk): """ Convert from roman numeral to base 10 (decimal) """ - if value=="0": + if value == "0": return 0L else: return fromroman(value) - def from_base(self,value,junk): + def from_base(self, value, junk): """ Convert from decimal to roman numeral """ - return toroman(value) + return toroman(int(value)) @@ -264,12 +267,16 @@ class function(object): #value is assumed to be a string #convert from a defined function to base - def to_base(self,value,(to_base,from_base)): - exec "y="+to_base[:string.find(to_base,'x')]+str(value)+to_base[string.find(to_base,'x')+1:] + def to_base(self, value, (to_base, from_base)): + leftOfX, rightOfX = to_base.split("x", 1) + y = 0 # "undefined" y was driving me nuts + exec "y=" + leftOfX + str(value) + rightOfX return y - def from_base(self,value,(to_base,from_base)): - exec "y="+from_base[:string.find(from_base,'x')]+str(value)+from_base[string.find(from_base,'x')+1:] + def from_base(self, value, (to_base, from_base)): + leftOfX, rightOfX = from_base.split("x", 1) + y = 0 # "undefined" y was driving me nuts + exec "y=" + leftOfX + str(value) + rightOfX return y