def temperature_series(self, unit='kelvin'): """Returns the temperature time series relative to the meteostation, in the form of a list of tuples, each one containing the couple timestamp-value :param unit: the unit of measure for the temperature values. May be among: '*kelvin*' (default), '*celsius*' or '*fahrenheit*' :type unit: str :returns: a list of tuples :raises: ValueError when invalid values are provided for the unit of measure """ if unit not in ('kelvin', 'celsius', 'fahrenheit'): raise ValueError("Invalid value for parameter 'unit'") result = [] for tstamp in self.station_history.measurements: t = self.station_history.measurements[tstamp]['temperature'] if unit == 'kelvin': temp = t elif unit == 'celsius': temp = measurables.kelvin_to_celsius(t) else: temp = measurables.kelvin_to_fahrenheit(t) result.append((tstamp, temp)) return result
async def weather(self, ctx, *, city): await ctx.message.delete() import pyowm from pyowm.commons.enums import SubscriptionTypeEnum from pyowm.utils.measurables import kelvin_to_celsius config = { 'subscription_type': SubscriptionTypeEnum.FREE, 'language': 'ru', 'connection': { 'use_ssl': True, 'verify_ssl_certs': True, 'use_proxy': False, 'timeout_secs': 5 }, 'proxies': { 'http': 'http://*****:*****@host:port', 'https': 'socks5://user:pass@host:port' } } owm = pyowm.OWM('a99967bc9ee70d5b4bd387902982f400', config=config) mgr = owm.weather_manager() observation = mgr.weather_at_place(city) w = observation.weather embed = discord.Embed(title="Bruh Bot", description="В городе " + city + " сейчас температура: " + str(kelvin_to_celsius(w.temp['temp'])) + "°C.", color=0x1100fd) embed.set_footer(text="Создатель команды - PirPix") await ctx.send(embed=embed)
def test_average_temperature_with_different_temperature_units(self): avg = (266.85 + 266.25)/2.0 expected_kelvin = avg expected_celsius = measurables.kelvin_to_celsius(avg) expected_fahrenheit = measurables.kelvin_to_fahrenheit(avg) self.assertEqual(expected_kelvin, self.__instance.average_temperature(unit='kelvin')) self.assertEqual(expected_celsius, self.__instance.average_temperature(unit='celsius')) self.assertEqual(expected_fahrenheit, self.__instance.average_temperature(unit='fahrenheit'))
def ten_cm_temp(self, unit='kelvin'): """Returns the soil temperature measured 10 cm below surface :param unit: the unit of measure for the temperature value. May be: '*kelvin*' (default), '*celsius*' or '*fahrenheit*' :type unit: str :returns: a float :raises: ValueError when unknown temperature units are provided """ if unit == 'kelvin': return self._ten_cm_temp if unit == 'celsius': return measurables.kelvin_to_celsius(self._ten_cm_temp) if unit == 'fahrenheit': return measurables.kelvin_to_fahrenheit(self._ten_cm_temp) else: raise ValueError('Wrong temperature unit')
def average_temperature(self, unit='kelvin'): """Returns the average value in the temperature series :param unit: the unit of measure for the temperature values. May be among: '*kelvin*' (default), '*celsius*' or '*fahrenheit*' :type unit: str :returns: a float :raises: ValueError when invalid values are provided for the unit of measure or the measurement series is empty """ if unit not in ('kelvin', 'celsius', 'fahrenheit'): raise ValueError("Invalid value for parameter 'unit'") average = self._average( self._purge_none_samples(self.temperature_series())) if unit == 'kelvin': return average elif unit == 'celsius': return measurables.kelvin_to_celsius(average) else: return measurables.kelvin_to_fahrenheit(average)
def max_temperature(self, unit='kelvin'): """Returns a tuple containing the max value in the temperature series preceded by its timestamp :param unit: the unit of measure for the temperature values. May be among: '*kelvin*' (default), '*celsius*' or '*fahrenheit*' :type unit: str :returns: a tuple :raises: ValueError when invalid values are provided for the unit of measure or the measurement series is empty """ if unit not in ('kelvin', 'celsius', 'fahrenheit'): raise ValueError("Invalid value for parameter 'unit'") maximum = max(self._purge_none_samples(self.temperature_series()), key=itemgetter(1)) if unit == 'kelvin': return maximum elif unit == 'celsius': return (maximum[0], measurables.kelvin_to_celsius(maximum[1])) else: return (maximum[0], measurables.kelvin_to_fahrenheit(maximum[1]))
def update_event(self, input_called=-1): temp_dict = self.input(0) if self.input(1) != 'kelvin': for key in list(temp_dict.keys()): item = temp_dict[key] if item is not None: if self.input(1) == 'celsius': temp_dict[key] = kelvin_to_celsius(item) elif self.input(1) == 'fahrenheit': temp_dict[key] = kelvin_to_fahrenheit(item) # temp_dict = kelvin_dict_to(temp_dict, self.input(1)) doesn't work with NoneType values -.- which happen to persist temp = temp_dict['temp'] temp_kf = temp_dict['temp_kf'] temp_max = temp_dict['temp_max'] temp_min = temp_dict['temp_min'] feels_like = temp_dict['feels_like'] self.set_output_val(0, temp) self.set_output_val(1, temp_kf) self.set_output_val(2, temp_min) self.set_output_val(3, temp_max) self.set_output_val(4, feels_like)
def test_kelvin_to_celsius(self): kelvin = 301.0 expected = 27.85 result = measurables.kelvin_to_celsius(kelvin) self.assertEqual(expected, result)
'use_ssl': True, 'verify_ssl_certs': True, 'use_proxy': False, 'timeout_secs': 5 }, 'proxies': { 'http': 'http://*****:*****@host:port', 'https': 'socks5://user:pass@host:port' } } owm = pyowm.OWM('d3d48a022e1d2f6dd90197f50795a4ab', config=config) mgr = owm.weather_manager() observation = mgr.weather_at_place(city) w = observation.weather temp = kelvin_to_celsius(w.temp['temp']) temp2 = int(temp) def convert_time_to_string(dt): return f"{dt.hour}:{dt.minute:02}" def convert_date_to_string(dt): return f"{dt.day}.{dt.month}.{dt.year}" def convert_weather_to_string(): return f"%d*" % temp2