Skip to content

Commit 0c88ffb

Browse files
Code in script tbweather.py is optimtzed
1 parent 0be5584 commit 0c88ffb

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

tb_weather/tbweather.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import telebot
2-
import pyowm
3-
import Database
1+
from pyowm.owm import OWM
2+
from pyowm.weatherapi25.observation import Observation
3+
from telebot import TeleBot
4+
from telebot.types import ReplyKeyboardMarkup
5+
from Database import YAML
46
from config import *
57

6-
bot = telebot.TeleBot(telegram_key)
7-
owm = pyowm.OWM(weather_key)
8-
mgr = owm.weather_manager()
8+
MAX_LEN_OF_PLACE = 20
9+
10+
bot = TeleBot(telegram_key)
11+
mgr = OWM(weather_key).weather_manager()
912
help_msg = "Команды:\n/place - Ввод названия города. \n/update - Обновление информации об погоде в текущем городе \n/current_place - Вывод текущего города. \n/help - Вывод справки по командам бота"
1013

11-
keyboard = telebot.types.ReplyKeyboardMarkup(True, True)
14+
keyboard = ReplyKeyboardMarkup(True, True)
1215
keyboard.row('/place', '/update','/current_place','/help')
1316

14-
db = Database.YAML()
17+
db = YAML()
1518

1619
@bot.message_handler(commands=['start'])
1720
def start_message(message):
@@ -49,7 +52,7 @@ def help_message(message):
4952
def current_place(message):
5053
bot.send_message(message.chat.id, db.get_geolocation(message.chat.id), reply_markup=keyboard)
5154

52-
def output_data(observation):
55+
def output_data(observation: Observation) -> str:
5356
w = observation.weather
5457
answear = f'В городе {observation.location.name} {w.detailed_status}\n'
5558
answear += f'Температура воздуха составляет {w.temperature("celsius")["temp"]} °C\n'
@@ -60,8 +63,8 @@ def output_data(observation):
6063
answear += f'Заход солнца: {w.sunset_time(timeformat="iso")}\n'
6164
return answear
6265

63-
def get_current_weather(place):
64-
if place == None or len(place) > 20:
66+
def get_current_weather(place: str) -> Observation or None:
67+
if place == None or len(place) > MAX_LEN_OF_PLACE:
6568
return None
6669
try:
6770
observation = mgr.weather_at_place(place)

0 commit comments

Comments
 (0)