Package screenlets :: Package plugins :: Module BaseConverter

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.BaseConverter

     1  from Convert import Converter
     2
     3  # An example of writing a (rather advanced) converter module. Please see also
     4  # the documentation of the converter class if you haven't done so.
     5
     6  # Every converter class MUST contain:
     7 -class BaseConverter(Converter):
     8          """A converter which converts numbers between decimal, hexadecimal and octal
     9          bases."""
    10
    11          # __name__: the name of the class
    12          __name__ = 'BaseConverter'
    13          # __title__: a short description to be shown in the menu and Options dialog
    14          __title__ = 'Dec / Hex / Oct'
    15          # other meta-info - only for those who read the sources :-)
    16          # not currently shown anywhere, but this can change in near future
    17          __author__ = 'Vasek Potocek'
    18          __version__ = '0.1'
    19
    20          # the number of fields and their captions
    21          num_fields = 3
    22          field_names = ['Dec', 'Hex', 'Oct']
    23
    24          # filter_key function - see ConvertScreenlet.py for details
    25 -        def filter_key(self, key):
    26                  if self.active_field == 0:
    27                          return key in '0 1 2 3 4 5 6 7 8 9'.split()
    28                  elif self.active_field == 1:
    29                          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()
    30                  elif self.active_field == 2:
    31                          return key in '0 1 2 3 4 5 6 7'.split()
    32
    33          # convert function - see ConvertScreenlet.py for details
    34 -        def convert(self):
    35                  bases = [10, 16, 8]
    36                  # read the current value in active field
    37                  val = int(self.values[self.active_field], bases[self.active_field])
    38                  # compute self.values in all fields
    39                  self.values[0] = str(val)
    40                  # strip off leading "0x" and make uppercase
    41                  self.values[1] = hex(val)[2:].upper()
    42                  self.values[2] = oct(val)[1:]
    43                  if not self.values[2]: # this can happen in oct
    44                          self.values[2] = '0'
    45                  # strip off trailing 'L' if the self.value falls into long
    46                  for i in range(3):
    47                          if self.values[i][-1] == 'L':
    48                                  self.values[i] = self.values[i][:-1]
    49