Package screenlets :: Package plugins :: Module TemperatureConverter

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.TemperatureConverter

     1  # coding=UTF-8
     2
     3  from Convert import Converter
     4
     5 -class TemperatureConverter(Converter):
     6          """A converter which converts temperature between Fahrenheit and Celsius."""
     7
     8          __name__ = 'TemperatureConverter'
     9          __title__ = 'Fahrenheit / Celsius'
    10          __author__ = 'Arnav Ghosh'
    11          __version__ = '0.2'
    12
    13          num_fields = 2
    14          field_names = [u'˚F', u'˚C']
    15
    16 -        def __init__(self):
    17                  self.active_field = 0
    18                  self.values = ['0', '0']
    19                  # 0˚F is not 0˚C, let's correct this by a call to convert()
    20                  # (this leaves '0' on the active field)
    21                  self.convert()
    22
    23 -        def filter_key(self, key):
    24                  if key.isdigit() or key == '+' or key == '-':
    25                          return True
    26                  elif key == '.':
    27                          return not ('.' in self.values[self.active_field])
    28                  else:
    29                          return False
    30
    31 -        def convert(self):
    32                  try:
    33                          val = float(self.values[self.active_field])
    34                  except:
    35                          val = 0         # This handles the case of a single '-' in input
    36                  if self.active_field == 0:
    37                          self.values[1] = '%.1f' % ((val - 32) / 1.8)
    38                  else:
    39                          self.values[0] = '%.1f' % ((val * 1.8) + 32)
    40