screenlets.plugins.TemperatureConverter

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

# Source Code for Module screenlets.plugins.TemperatureConverter

     1  # coding=UTF-8
     2
     3  # This program is free software: you can redistribute it and/or modify
     4  # it under the terms of the GNU General Public License as published by
     5  # the Free Software Foundation, either version 3 of the License, or
     6  # (at your option) any later version.
     7  #
     8  # This program is distributed in the hope that it will be useful,
     9  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  # GNU General Public License for more details.
    12  #
    13  # You should have received a copy of the GNU General Public License
    14  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15
    16  from Convert import Converter
    17
    18 -class TemperatureConverter(Converter):
    19          """A converter which converts temperature between Fahrenheit and Celsius."""
    20
    21          __name__ = 'TemperatureConverter'
    22          __title__ = 'Fahrenheit / Celsius'
    23          __author__ = 'Arnav Ghosh'
    24          __version__ = '0.2'
    25
    26          num_fields = 2
    27          field_names = [u'˚F', u'˚C']
    28
    29 -        def __init__(self):
    30                  self.active_field = 0
    31                  self.values = ['0', '0']
    32                  # 0˚F is not 0˚C, let's correct this by a call to convert()
    33                  # (this leaves '0' on the active field)
    34                  self.convert()
    35
    36 -        def filter_key(self, key):
    37                  if key.isdigit() or key == '+' or key == '-':
    38                          return True
    39                  elif key == '.':
    40                          return not ('.' in self.values[self.active_field])
    41                  else:
    42                          return False
    43
    44 -        def convert(self):
    45                  try:
    46                          val = float(self.values[self.active_field])
    47                  except:
    48                          val = 0         # This handles the case of a single '-' in input
    49                  if self.active_field == 0:
    50                          self.values[1] = '%.1f' % ((val - 32) / 1.8)
    51                  else:
    52                          self.values[0] = '%.1f' % ((val * 1.8) + 32)
    53