Zuzu
Zuzu

Reputation: 3

Python class keeps giving me the same value

Hi im new to Python and im trying to figure out python classes. I have made the following script, and I don't understand when I make the object vejr, outside the while loop, it keeps printing the same value over and over again. If i make the object inside the while loop it works and print the temperature as it changes. Inside the loop I make a new vejr object every time the loop runs, I thought it would be enough just make the vejr object once?? or is there a better way to write code like this?

import requests

import json

from datetime import datetime

import time


class Owm_data():

    def __init__(self, api_key="api_key", lat="51.507351", lon="-0.127758" ):
        
        self.api_key = api_key 
        self.lat = lat
        self.lon = lon  
        self.url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s&units=metric" % (lat, lon, api_key)
        self.response = requests.get(self.url)
        self.data = json.loads(self.response.text)
          

    def get_temperature(self):
        return self.data["current"]["temp"]

    def get_pressure(self):
        return self.data["current"]["pressure"]



vejr = Owm_data()

while True:

    print(vejr.get_temperature())

    time.sleep(30)

Upvotes: 0

Views: 81

Answers (2)

Acccumulation
Acccumulation

Reputation: 3591

I recommend distinguishing between temperature as an attribute versus a method. For instance:

class Owm_data():

    def __init__(self, api_key="api_key", lat="51.507351", lon="-0.127758" ):
        
        self.api_key = api_key 
        self.lat = lat
        self.lon = lon  
        self.temperature = self.get_temperature()

    def get_temperature(self):
        self.url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s&units=metric" % (lat, lon, api_key)
        self.response = requests.get(self.url)
        self.data = json.loads(self.response.text)
        self.temperature = self.data["current"]["temp"]
        return self.temperature

This way, if you want to access the cached value of the temperature, you can do print(vejr.temperature). If you want to update the value of the temperature, you can do print(vejr.get_temperature()). Although you might want to use a name that makes it clear that the temperature is being updated, such as update_temperature().

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155506

You pull the data when you initialize the class, and you only do that outside the loop. So your vejr.get_temperature() call is just reproducing the same cached information you saw at construction. If you want to recheck the data each loop, change from:

vejr = Owm_data()

while True:
    print(vejr.get_temperature())
    time.sleep(30)

to:

while True:
    vejr = Owm_data()
    print(vejr.get_temperature())
    time.sleep(30)

so the class is reconstructed on each loop and repulls the data.

Other solutions might include pulling the data each call to get_temperature and friends, rather than doing it in the initializer, but leaving the design as is is more useful when you want cached data so you can see both temperature and pressure from a single moment of time with one call.

Upvotes: 1

Related Questions