Récupérer la valeur d'une fonction dans une autre fonction
Bonjour à tous,
J'essaie de faire une station météo pour mon raspberry. Dans ma 1ère version, tout fonctionnait très sauf que mon timer de rappel etait trop court (0.4sec) donc je prenais des timeout de la part du serveur de l'API météo.
J'ai repensé tous le code. je me retourve donc avec 3 fichiers (que j'aimerais gardé comme ça) le premier sert à la mise a jour du fichier JSON, le 2eme, à la mise a jour de la vitesse du vent et le 3eme à l'affichage à l'écran.
Tout fonctionne bien SAUF, l'affichage du JSON.
Voici le fichier de mise à jour du JSON:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
# -- Python modules
import pygame as pg
import requests as rq
from time import sleep, strftime
# -- Personnal modules
from ui import loop, mode, constants as cst
from timestamp import lclock
from weather import wind
from os import system
class WeatherUpdate:
def __init__(self, screen):
self.screen = screen
self.lisawind = wind.LSWind(self.screen)
self.display_mode = mode.DisplayMode(self.screen)
def json_request(self):
self.api_link = "https://api.openweathermap.org/data/2.5/weather?q=castelnau-le-lez&lang=fr&appid=738e9d1489491771d579f8b5db5fd21a"
self.json_data = rq.get(self.api_link).json()
print("server request")
return self.json_data
def full_update(self, counter):
for _ in range(6):
self.lisawind.wind_request(self.json_request())
self.display_mode.time_slot()
counter.loop_increase()
print(counter.get_loop_value())
if counter.get_loop_value() == 20:
self.lisawind.wind_request(self.weather_request())
counter.loop_value = 0
sleep(0.5)
system('clear') |
Voici le fichier de mise a jour de la vitesse du vent
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
# -- Python modules
import pygame as pg
# -- Personnal modules
from ui import constants as cst
class LSWind:
def __init__(self, screen):
self.screen = screen
self.w_font = pg.font.SysFont("", 50)
def wind_request(self, json_data):
self.w_speed = json_data['wind']['speed']
def wind_display(self, color):
self.w_km = " Km/h"
self.w_current_speed = int(self.w_speed * 3600 / 1000)
self.w_display = str(self.w_current_speed) + self.w_km
self.screen.master.blit(cst.WIND_FLAG, (630, 318))
self.show_w = self.w_font.render(self.w_display, True, color)
self.center_y = (344 - (self.show_w.get_height() / 2) + 5)
self.screen.master.blit(self.show_w, (700, self.center_y))
return self.w_display |
Et voici le fichier d'affichage à l'écran
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
# -- Python modules
import pygame as pg
from time import strftime
# -- Personnal modules
from ui import constants as cst
from timestamp import lclock, ldate
from weather import wind
class DisplayMode:
def __init__(self, screen):
self.screen = screen
self.lisaclock = lclock.LSClock(self.screen)
self.lisadate = ldate.LSDate(self.screen)
self.lisawind = wind.LSWind(self.screen)
def time_slot(self):
self.hour = strftime("%H")
self.mn = strftime("%M")
self.current_time = strftime("%H:%M")
self.screen.master.blit(cst.BACKGROUND, (0, 0))
if "09" <= self.hour <= "21":
self.light()
else:
self.dark()
pg.display.flip()
def light(self):
self.lisaclock.centering(self.current_time, cst.GREEN)
self.lisadate.centering(cst.CYAN)
self.lisawind.wind_display(cst.WHITE)
def dark(self):
self.lisaclock.centering(self.current_time, cst.GREY)
self.lisadate.centering(cst.GREY)
self.lisawind.wind_display(cst.GREY) |
Concrètement, mon problème vient du fait que dans la fonction d'affichage 'wind_display()', j'appelle "self.w_speed" contenu dans la fonction "wind_request()" or, ile me donne ce message d'erreur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
Traceback (most recent call last):
File "LisaClock.py", line 34, in <module>
main()
File "LisaClock.py", line 27, in main
lisawupd.full_update(lisaloop)
File "/home/priyamoon/RASPBERRY/PROJECTS/LisaClock/updates/wupdate.py", line 38, in full_update
self.display_mode.time_slot()
File "/home/priyamoon/RASPBERRY/PROJECTS/LisaClock/ui/mode.py", line 33, in time_slot
self.light()
File "/home/priyamoon/RASPBERRY/PROJECTS/LisaClock/ui/mode.py", line 42, in light
self.lisawind.wind_display(cst.WHITE)
File "/home/priyamoon/RASPBERRY/PROJECTS/LisaClock/weather/wind.py", line 28, in wind_display
self.w_current_speed = int(self.w_speed * 3600 / 1000)
AttributeError: 'LSWind' object has no attribute 'w_speed' |
Ma question est donc : Comment puis-je utiliser la valeur du JSON dans ma fonction d'affichage ?
Désolé c'est long mais je voulais être sûr d're assez clair.
Merci à vous :)