Package screenlets :: Package plugins :: Module BaseConverter
[hide private]
[frames] | no frames]

Source Code for Module screenlets.plugins.BaseConverter

 1  # This program is free software: you can redistribute it and/or modify 
 2  # it under the terms of the GNU General Public License as published by 
 3  # the Free Software Foundation, either version 3 of the License, or 
 4  # (at your option) any later version. 
 5  #  
 6  # This program is distributed in the hope that it will be useful, 
 7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 9  # GNU General Public License for more details. 
10  #  
11  # You should have received a copy of the GNU General Public License 
12  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
13   
14  from Convert import Converter 
15   
16  # An example of writing a (rather advanced) converter module. Please see also  
17  # the documentation of the converter class if you haven't done so. 
18   
19  # Every converter class MUST contain: 
20 -class BaseConverter(Converter):
21 """A converter which converts numbers between decimal, hexadecimal and octal 22 bases.""" 23 24 # __name__: the name of the class 25 __name__ = 'BaseConverter' 26 # __title__: a short description to be shown in the menu and Options dialog 27 __title__ = 'Dec / Hex / Oct' 28 # other meta-info - only for those who read the sources :-) 29 # not currently shown anywhere, but this can change in near future 30 __author__ = 'Vasek Potocek' 31 __version__ = '0.1' 32 33 # the number of fields and their captions 34 num_fields = 3 35 field_names = ['Dec', 'Hex', 'Oct'] 36 37 # filter_key function - see ConvertScreenlet.py for details
38 - def filter_key(self, key):
39 if self.active_field == 0: 40 return key in '0 1 2 3 4 5 6 7 8 9'.split() 41 elif self.active_field == 1: 42 return key in '0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f'.split() 43 elif self.active_field == 2: 44 return key in '0 1 2 3 4 5 6 7'.split()
45 46 # convert function - see ConvertScreenlet.py for details
47 - def convert(self):
48 bases = [10, 16, 8] 49 # read the current value in active field 50 val = int(self.values[self.active_field], bases[self.active_field]) 51 # compute self.values in all fields 52 self.values[0] = str(val) 53 # strip off leading "0x" and make uppercase 54 self.values[1] = hex(val)[2:].upper() 55 self.values[2] = oct(val)[1:] 56 if not self.values[2]: # this can happen in oct 57 self.values[2] = '0' 58 # strip off trailing 'L' if the self.value falls into long 59 for i in range(3): 60 if self.values[i][-1] == 'L': 61 self.values[i] = self.values[i][:-1]
62