[modules/weather] Small improvements
This commit is contained in:
parent
a7effbff78
commit
a71828f0b4
4 changed files with 21 additions and 21 deletions
|
@ -35,5 +35,3 @@
|
|||
- themes: use colors to improve theme readability
|
||||
- brightness: read from CLI tools
|
||||
- input: use events?
|
||||
- themes: rotating icons (battery!)
|
||||
- weather: improve code
|
||||
|
|
|
@ -24,11 +24,8 @@ import util.format
|
|||
|
||||
import re
|
||||
|
||||
try:
|
||||
import requests
|
||||
from requests.exceptions import RequestException
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
class Module(core.module.Module):
|
||||
@core.decorators.every(minutes=15)
|
||||
|
@ -50,29 +47,20 @@ class Module(core.module.Module):
|
|||
|
||||
def __next_location(self, event):
|
||||
self.__index = (self.__index + 1) % len(self.__location)
|
||||
self.update(self.widgets())
|
||||
self.update()
|
||||
|
||||
def __prev_location(self, event):
|
||||
self.__index = len(self.__location) - 1 if self.__index <= 0 else self.__index - 1
|
||||
self.update(self.widgets())
|
||||
|
||||
def __unit_suffix(self):
|
||||
if self.__unit == 'metric':
|
||||
return 'C'
|
||||
if self.__unit == 'kelvin':
|
||||
return 'K'
|
||||
if self.__unit == 'imperial':
|
||||
return 'F'
|
||||
return ''
|
||||
self.update()
|
||||
|
||||
def temperature(self):
|
||||
return u'{}°{}'.format(self.__temperature, self.__unit_suffix())
|
||||
return util.format.astemperature(self.__temperature, self.__unit)
|
||||
|
||||
def tempmin(self):
|
||||
return u'{}°{}'.format(self.__tempmin, self.__unit_suffix())
|
||||
return util.format.astemperature(self.__tempmin, self.__unit)
|
||||
|
||||
def tempmax(self):
|
||||
return u'{}°{}'.format(self.__tempmax, self.__unit_suffix())
|
||||
return util.format.astemperature(self.__tempmax, self.__unit)
|
||||
|
||||
def city(self):
|
||||
city = re.sub('[_-]', ' ', self.__city)
|
||||
|
@ -113,6 +101,7 @@ class Module(core.module.Module):
|
|||
weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid={}'.format(self.__apikey)
|
||||
weather_url = '{}&units={}'.format(weather_url, self.__unit)
|
||||
if self.__location[self.__index] == 'auto':
|
||||
# TODO: make that a general "provider"
|
||||
location_url = 'http://ipinfo.io/json'
|
||||
location = requests.get(location_url).json()
|
||||
coord = location['loc'].split(',')
|
||||
|
|
|
@ -88,4 +88,10 @@ class format(unittest.TestCase):
|
|||
|
||||
self.assertEqual(4*3600 + 5*60, seconds('4h5m'))
|
||||
|
||||
def test_temperature(self):
|
||||
self.assertEqual('10°C', astemperature(10))
|
||||
self.assertEqual('10°C', astemperature(10, 'metric'))
|
||||
self.assertEqual('-100°F', astemperature(-100, 'imperial'))
|
||||
self.assertEqual('-100°K', astemperature('-100', 'kelvin'))
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -23,6 +23,13 @@ def aslist(val):
|
|||
return val
|
||||
return str(val).replace(' ', '').split(',')
|
||||
|
||||
__UNITS = {
|
||||
'metric': 'C', 'kelvin': 'K', 'imperial': 'F',
|
||||
'default': 'C'
|
||||
}
|
||||
def astemperature(value, unit='metric'):
|
||||
return u'{}°{}'.format(int(value), __UNITS.get(unit, __UNITS['default']))
|
||||
|
||||
def byte(val, fmt='{:.2f}'):
|
||||
for unit in ['', 'Ki', 'Mi', 'Gi']:
|
||||
if val < 1024.0:
|
||||
|
|
Loading…
Reference in a new issue