Python Forum

Full Version: Using a variable to replace an integer? (Except it isn't working!)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
So I'm trying to use the yweather module, but replacing the way it goes about getting the woeid with a system of variables so that I can A. use it wherever and not have to input a new woeid, and B. automate the whole thing. I'm super new to python but I feel like this shouldn't be this difficult, here's my code:

#note: the url is actually there in the code, but it was preventing me from posting this
url = 'freegeoip'
try:
     with closing(urlopen(url)) as response:
           location = json.loads(response.read())
           loc_c = location['city']
           loc_s = location['region_name']
except:
           print('step 1 failed')

client = yweather.Client()
wid = client.fetch_woeid('loc_c, loc_s')
print(wid)
print('steps 1 and 2 finished')

print('step 3 started')
loc_weather = client.fetch_weather('wid')
loc_weather["condition"]["text"]
loc_weather["condition"]["temp"]
print('step 3 finished')
The whole thing works just fine, up until "loc_weather...('wid')". Then it just stops, I think? It doesn't give me an error message in the terminal or anything, it just stops firing I think. It works fine when I put in the actual woe id it wants, but that isn't going to work if I'm in a new place / using an automated script. Any help would be appreciated! Sorry if this is a dumb question, I'm super new to python.
You are using this wrong.
Surrounding it with single quotes makes it a string.

The original looks like:
WOEID_LOOKUP_URL = ("http://locdrop.query.yahoo.com/v1/public/yql?"
                   "q=select%20woeid%20from%20locdrop.placefinder%20"
"where%20text='{0}'")
so in order to assign this to url, it needs to be:
url = WOEID_LOOKUP_URL.format('freegeoip')
Output:
"http://locdrop.query.yahoo.com/v1/public/yql?q=select%20woeid%20from%20locdrop.placefinder%20where%20text='freegeoip'"
Now you've got a valid url!

this url may have changed, see: https://developer.yahoo.com/geo/placefinder/
I appreciate the response! But unless I'm misunderstanding what you're saying, I don't think that's the problem. Like I said in that note at the top of the code, there is a complete url in the actual script (I couldn't put it in because this is my first post and the site restricts me) and the parts of the script that rely on it (step 1 and to a lesser degree step 2) function perfectly. The part of the script that I think is the problem area is step 3, I'm just not sure why. That said, I'm pulling it up on my linux machine right now and trying it, so I'll be sure to keep you posted!

UPDATE: Yeah I think I was right, there was definitely some miscommunication somewhere down the line 'cause that didn't work either, haha!
I really don't know what all the inputs are,
nor what the output should be, as you were not clear on this.

I actually went to the github location for yweather and attempted to figure out what you were trying to do.
I'm still not quite sure exactly what you are trying to accomplish.
I'm basically putting two pieces of code together. The first one (step 1) gets your location based on your ip address. I have it turn the location it gets into variables that I can put into other things. (Loc_C for the city it outputs, Loc_S for the state.) This part works perfectly! But you can't get the weather with yweather using that kind of location data, so the script has to use the "fetch_woeid" function. (This is step 2.) I use the variables from step 1 as the location data of the location for which I'm fetching the woeid, and it returns a string of numbers for me to put into the module's other commands. Now up until this point, everything works just peachy keen, and if I directly (and unfortunately manually) input the woeid into the "fetch_weather" command, everything continues to work. The problem is when I try and equate the woeid step 2 returns into a variable, in order to automate the process. (If I can just say "this returned bit of data, the woeid = wid" and have the fetch_weather command fetch "wid" instead of an actual woeid, I won't need to type the thing out every time, and I can just make it part of the script.) And, in theory at least, this should work, because it's almost exactly what I did in steps 1 and 2, with the loc_c and loc_s variables. Except it doesn't. And I don't get any helpful error messages, the whole thing just stops firing. If I want to use my terminal again I have to ctrl-z to end the program. Is that more helpful? Like I said, I'm super new to this, so I don't 100% know what I'm doing/talking about!
I think this

loc_weather = client.fetch_weather('wid')
should be

loc_weather = client.fetch_weather(wid)
the docs:

https://yweather.readthedocs.io/en/v0.1/...-s-weather
(Jul-25-2017, 06:55 AM)buran Wrote: [ -> ]I think this

loc_weather = client.fetch_weather('wid')
should be

loc_weather = client.fetch_weather(wid)
the docs:

https://yweather.readthedocs.io/en/v0.1/...-s-weather

Haha I wish, nah I already tried that. Also tried using quotes, still no dice. Thanks for the attempt though!
(Jul-25-2017, 07:00 AM)s1monsays Wrote: [ -> ]Haha I wish, nah I already tried that.

Well, post your attempt at this, because it's exactly what should be done... Don't forget to post also any full traceback you get... in proper tags
wid returned by client.fetch_woeid is a str:
example from the docs

>>> client.fetch_woeid("Beijing, China")
'2151330'
and again fetch the weather example from the docs

>>> beijing_weather = client.fetch_weather("2151330")
>>> beijing_weather["guid"]
'CHXX0008_2013_01_06_7_00_CST'
>>> beijing_weather["description"]
'Yahoo! Weather for Beijing, CN'
>>> beijing_weather["condition"]["temp"]
'28'

I just missed that
wid = client.fetch_woeid('loc_c, loc_s')
should be

wid = client.fetch_woeid("{}, {}".format(loc_c, loc_s))
I'm not sure what you mean by "full attempt". Just like, all of the code I used? Because it's literally the same - that one change. And as for the traceback, I don't get any! It's kind of part of the problem. Also, changing the fetch_woeid thing like that makes that part not work either. I actually do get traceback from that bit though: "typeerror: fetch_woeid() takes exactly 2 arguments (3 given)".
(Jul-25-2017, 07:16 AM)s1monsays Wrote: [ -> ]I'm not sure what you mean by "full attempt"

Actually I said:

(Jul-25-2017, 07:16 AM)s1monsays Wrote: [ -> ]Well, post your attempt at this, because it's exactly what should be done... Don't forget to post also any full traceback you get... in proper tags

That means

Post your code in code tags.

post your full traceback in error tags, not just "typeerror: fetch_woeid() takes exactly 2 arguments (3 given)"
Pages: 1 2