File: conversions.py

package info (click to toggle)
pythoncard 0.8.1-8.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 5,352 kB
  • ctags: 4,594
  • sloc: python: 42,401; makefile: 55; sh: 22
file content (168 lines) | stat: -rw-r--r-- 5,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python

"""
__version__ = "$Revision: 1.14 $"
__date__ = "$Date: 2004/05/05 16:53:25 $"
"""

from PythonCard import model

class Conversion:
    def __init__(self, components):
        pass

    def toDown(self, value):
        pass

    def toUp(self, value):
        pass
    
class TemperatureConversion(Conversion):
    def __init__(self, components):
        components.labelUp.text = 'Fahrenheit'
        components.btnConvertUp.label = 'Celsius to Fahrenheit'
        components.btnConvertDown.label = 'Fahrenheit to Celsius'
        components.labelDown.text = 'Celsius'

    def toDown(self, degrees):
        return str(self.FahrenheitToCelsius(float(degrees)))

    def toUp(self, degrees):
        return str(self.CelsiusToFahrenheit(float(degrees)))

    def FahrenheitToCelsius(self, degrees):
        return (degrees - 32.0) / 9.0 * 5.0

    def CelsiusToFahrenheit(self, degrees):
        return degrees * 9.0 / 5.0 + 32.0

class MorseCodeConversion(Conversion):
    def __init__(self, components):
        """
        A B C D E F G
        H I J K L M N
        O P Q R S T U
        V W X Y Z
        """
        self.morseAlphabet = ['.-', '-...', '-.-.', '-..', '.', '..-.', '--.',
                              '....', '..', '.---', '-.-', '.-..', '--', '-.',
                              '---', '.--.', '--.-', '.-.', '...', '-', '..-',
                              '...-', '.--', '-..-', '-.--', '--..']
        
        components.labelUp.text = 'English'
        components.btnConvertUp.label = 'Morse code to English'
        components.btnConvertDown.label = 'English to Morse code'
        components.labelDown.text = 'Morse code'

    def toDown(self, txt):
        return self.convertToMorse(txt)

    def toUp(self, txt):
        return self.convertFromMorse(txt)

    def convertToMorse(self, txt):
        a = ord('A')
        z = ord('Z')
        converted = ''
        for c in txt[:]:
            ordC = ord(c.upper())
            if c == ' ':
                # three spaces between words
                # when you include the space after each character
                converted = converted + '  '
            elif ordC < a or ordC > z:
                return converted + "\n\ncharacter out of bounds, unable to complete conversion"
            else:
                converted += self.morseAlphabet[ordC - a] + ' '
        return converted[:-1]

    def convertFromMorse(self, txt):
        a = ord('A')
        converted = ''
        words = txt.split('  ')
        for w in words:
            letters = w.split(' ')
            for c in letters:
                if c == '':
                    continue
                try:
                    ordC = self.morseAlphabet.index(c)
                except:
                    return converted + "\n\nmorse out of bounds error, unable to complete conversion"
                converted += chr(ordC + a)
            converted += ' '
        return converted

class CurrencyConversion(Conversion):
    def __init__(self, components):
        components.labelUp.text = 'Aussie Dollars'
        components.btnConvertUp.label = 'US Dollars to Aussie Dollars'
        components.btnConvertDown.label = 'Aussie Dollars to US Dollars'
        components.labelDown.text = 'US Dollars'

    def toDown(self, txt):
        return self.convertToUS(txt)

    def toUp(self, txt):
        return self.convertToAus(txt)

    def convert(self, fromCur, toCur, txt):
        import SOAP
        server = SOAP.SOAPProxy('http://services.xmethods.net/soap', \
                        namespace='urn:xmethods-CurrencyExchange')
        try:
            dummy = float(txt)
        except:
            return "Cannot convert anything but numbers"
        try:
            rate = server.getRate(fromCur, toCur)
        except:
            return "Error getting exchange rate"
        return str(float(txt) * rate)

    def convertToUS(self, txt):
        return self.convert('Australia', 'US', txt)

    def convertToAus(self, txt):
        return self.convert('US', 'Australia', txt)

class Conversions(model.Background):

    def on_initialize(self, event):
        self.conversion = TemperatureConversion(self.components)

    def on_btnConvertDown_mouseClick(self, event):
        self.components.field2.text = self.conversion.toDown(self.components.field1.text)

    def on_btnConvertUp_mouseClick(self, event):
        self.components.field1.text = self.conversion.toUp(self.components.field2.text)
        
    def on_menuConvertMorseCode_select(self, event):
        self.conversion = MorseCodeConversion(self.components)
        self.menuBar.setChecked('menuConvertMorseCode')
        self.menuBar.setChecked('menuConvertTemperature', 0)
        self.menuBar.setChecked('menuConvertCurrency', 0)

    def on_menuConvertTemperature_select(self, event):
        self.conversion = TemperatureConversion(self.components)
        self.menuBar.setChecked('menuConvertMorseCode', 0)
        self.menuBar.setChecked('menuConvertTemperature')
        self.menuBar.setChecked('menuConvertCurrency', 0)

    def on_menuConvertCurrency_select(self, event):
        "We only enable this option if the SOAP module is installed"
        try:
            import SOAP
        except ImportError:
            self.menuBar.setChecked('menuConvertCurrency', 0) 
            self.menuBar.setEnabled('menuConvertCurrency', 0)
            return
        self.conversion = CurrencyConversion(self.components)
        self.menuBar.setChecked('menuConvertMorseCode', 0)
        self.menuBar.setChecked('menuConvertTemperature', 0)
        self.menuBar.setChecked('menuConvertCurrency')


if __name__ == '__main__':
    app = model.Application(Conversions)
    app.MainLoop()