Jun-16-2020, 03:12 PM
Hi everyone,
I would like to know how to use function result in another function.. I explain..
I created a function to retrieve weather data in a JSON file.
Here is the code from weather.py:
So I have to do a conversion, which I know, but the problem comes from the recovery of the value contained in the JSON file.
Here is the call code from update.py
If i call the function name (wind_request()), it ask me a parameter, of course and if I call the (self.w_speed) variable, here is the error message
I would like to know how to use function result in another function.. I explain..
I created a function to retrieve weather data in a JSON file.
Here is the code from weather.py:
def weather_request(self):
self.api_link = "https://api.openweathermap.org/data/2.5/weather?q=castelnau-le-lez&lang=fr&appid=738e9d1489491771d579f8b5db5fd21a"
self.json_data = requests.get(self.api_link).json()
return self.json_dataSo far, no problem, I know it works, but the wind speed is in m/s, I want it in km/s. So I have to do a conversion, which I know, but the problem comes from the recovery of the value contained in the JSON file.
Here is the call code from update.py
# -- Python modules
import pygame as pg
import requests
# -- 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']
return self
def wind_display(self, color):
self.w_km = " Km/h"
self.w_current_speed = int(self.wind_request() * 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"def wind_request" call the json file from (weather.py) but I don't know what to use for convert wind speed.If i call the function name (wind_request()), it ask me a parameter, of course and if I call the (self.w_speed) variable, here is the error message
Error:self.w_current_speed = int(self.w_speed * 3600 / 1000)
AttributeError: 'LSWind' object has no attribute 'w_speed'Anyone have an idea please ? Thank you
