Favorites support
[gonvert] / src / converters.py
index 90001f1..d53c768 100644 (file)
@@ -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,
 }
 
 
@@ -72,19 +72,19 @@ def toroman(n):
        if n < 0:
                raise NotImplementedError("Value out of roman comprehension")
        elif n == 0:
-               ''
+               pass
        elif n >= 4000:
                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,10 +222,13 @@ 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:
@@ -233,29 +236,29 @@ class base_converter(object):
                        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,11 +267,13 @@ 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)):
+       def to_base(self, value, (to_base, from_base)):
+               y = 0 # "undefined" y was driving me nuts
                exec "y="+to_base[:to_base.find('x')]+str(value)+to_base[to_base.find('x')+1:]
                return y
 
-       def from_base(self,value,(to_base,from_base)):
+       def from_base(self, value, (to_base, from_base)):
+               y = 0 # "undefined" y was driving me nuts
                exec "y="+from_base[:from_base.find('x')]+str(value)+from_base[from_base.find('x')+1:]
                return y