Refactoring color_temperature function

This commit is contained in:
2025-09-03 18:47:38 +02:00
parent a7d085e607
commit 76aa1195ef

29
main.py
View File

@@ -7,11 +7,12 @@ import argparse
import requests
from pathlib import Path
from datetime import datetime
from sty import fg, bg, ef, rs
from sty import fg
from meteo.config import Config
YR_NO_URL = "https://api.met.no/weatherapi/locationforecast/2.0/"
def load_args():
"""Charge l'action et les paramêtres communs a toutes les actions."""
parser = argparse.ArgumentParser(description="Affiche la météo")
@@ -63,7 +64,7 @@ def display_full(data):
wind_speed = infos["data"]["instant"]["details"]["wind_speed"]
wind_direction = diplay_wind_direction(
wind_direction = display_wind_direction(
infos["data"]["instant"]["details"]["wind_from_direction"]
)
@@ -71,7 +72,7 @@ def display_full(data):
print(f" {time} : {temperature}° / {cur_humidity}% / {wind_direction} {wind_speed} m/s")
def diplay_wind_direction(degrees: float) -> str:
def display_wind_direction(degrees: float) -> str:
""" Retourne une fleche indicant la direction du vent"""
if 22.5 <= degrees < 67.5:
return ''
@@ -99,25 +100,15 @@ def diplay_wind_direction(degrees: float) -> str:
def coloring_temperature(temperature: float) -> str:
""" A partir de la température retourne une chaine de caractères colorisée """
if temperature >= 40.0:
return fg.magenta + str(temperature) + fg.rs
if temperature >= 30.0:
return fg.red + str(temperature) + fg.rs
temp_limit = [40, 30, 25, 20, 10, 0]
color = [fg.magenta, fg.red, fg(255, 150, 50), fg.yellow, '', fg.cyan]
if temperature >= 25.0:
return fg(255, 150, 50) + str(temperature) + fg.rs
for key, limit in enumerate(temp_limit):
if temperature >= limit:
return color[key] + str(temperature) + fg.rs
if temperature >= 20.0:
return fg.yellow + str(temperature) + fg.rs
if temperature >= 10.0:
return str(temperature)
if temperature >= 0.0:
return fg.cyan + str(temperature) + fg.rs
return fg.blue + str(cur_temp) + fg.rs
return fg.blue + str(temperature) + fg.rs
def main():