Python Forum
How to use function result in another function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use function result in another function
#1
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:
    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_data
So 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
Reply
#2
You are on the right track with return functions.
def add_10(num):
    result = num + 10
    return result

total = add_10(5)
print(total, add_10(5))
This little example shows how you send info to one function and get it back in a variable that you can send to the next function if you want.
Reply
#3
weather has not been imported into update
weather_request is a method of a class, you would need to make an instance of that class to call weather_request.
(although looking at it, there is nothing coming from elsewhere in the class)
The result of this you would pass as an attribute to the method wind_request
This will create the attribute self.w_speed (return self is not required)
then you can call self.w_current_speed = int(self.w_speed * 3600 / 1000)
Reply
#4
There's nothing showing how you're running the code and the error message has different code than the code given. Going off the error code, it looks like wind_display is being run before win_request causing for that error to happen because the variable has yet to become an attribute of the function. Either you can run it wherever you're running this other code, or you can pass json_data to the display function and call the wind_request from wind_display. You can also call weather_request from weather_display to get the json_data and use that json_data for wind_request, though since it looks like that function comes from a different class you'd have to pass the class as an argument. Now based on the code given, if you want to call wind_request from inside wind_display then it'll need to return self.w_speed, then you'd also yer again need the json_data to be passed into wind_display.
Hope this helps
Reply
#5
Thank you very much for all your replies..
I will try all of this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 622 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Exit function from nested function based on user input Turtle 5 2,895 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,664 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 3,519 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Passing argument from top-level function to embedded function JaneTan 2 2,236 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  unable to use result of solver in another function ross1993hall 0 1,413 Aug-10-2020, 10:29 AM
Last Post: ross1993hall
  use NULL as function parameter which is a pointer function? oyster 0 2,501 Jul-11-2020, 09:02 AM
Last Post: oyster
  Pandas's regular expression function result is so strange cools0607 6 3,166 Jun-15-2020, 07:34 AM
Last Post: cools0607
  Custom function that prints function text gyaan_anveyshak 3 2,090 Jan-28-2020, 05:43 AM
Last Post: perfringo
  Unrelated new function causing existing working function to fail Nick_G 2 2,248 Jan-27-2020, 07:21 PM
Last Post: Nick_G

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020