From a71828f0b411d287ea9ce7b5f65d60668b873e43 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sat, 4 Apr 2020 07:47:53 +0200 Subject: [PATCH] [modules/weather] Small improvements --- doc/NOTES.md | 2 -- modules/contrib/weather.py | 27 ++++++++------------------- tests/util/test_format.py | 6 ++++++ util/format.py | 7 +++++++ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/doc/NOTES.md b/doc/NOTES.md index 30098c3..4440fe5 100644 --- a/doc/NOTES.md +++ b/doc/NOTES.md @@ -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 diff --git a/modules/contrib/weather.py b/modules/contrib/weather.py index 6c0bcf1..6e3c88f 100644 --- a/modules/contrib/weather.py +++ b/modules/contrib/weather.py @@ -24,11 +24,8 @@ import util.format import re -try: - import requests - from requests.exceptions import RequestException -except ImportError: - pass +import requests +from requests.exceptions import RequestException 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(',') diff --git a/tests/util/test_format.py b/tests/util/test_format.py index f31df19..70b9761 100644 --- a/tests/util/test_format.py +++ b/tests/util/test_format.py @@ -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 diff --git a/util/format.py b/util/format.py index ef64343..c30751e 100644 --- a/util/format.py +++ b/util/format.py @@ -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: