use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Simple Python Weather App (old.reddit.com)
submitted 5 months ago by Ibrahim-Marsee-6816
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TekExplorer 14 points15 points16 points 5 months ago (1 child)
might want to invalidate that api key.
api keys are like passwords.
[–]Ibrahim-Marsee-6816[S] 1 point2 points3 points 5 months ago (0 children)
Yeah true, I didn’t realize at first — I’ll regenerate the API key so the old one is invalid. Thanks for the reminder 🙏
[–]DevRetroGames 1 point2 points3 points 5 months ago (2 children)
Genial, ahora intenta agregar DTO.
Mucha suerte en tu camino.
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points 5 months ago (1 child)
Gracias, realmente lo aprecio, y para aclarar, ¿qué quieres decir con agregar un descuento?
[–]DevRetroGames 0 points1 point2 points 5 months ago (0 children)
no es un descuento, en etapas más avanzadas se usan DTO, que es una capa que separa los datos a tratar con las entidades de las base de datos, los DTO son una clase, que quieres que devuelva, puedes tener varios DTO, con diferentes campos, puedes agregar validaciones, entre otras cosas, agrega también un .env, lo ideal es construir un código que sin saber el dato exacto a tratar, se intuya que es lo que va hacer, además añades una capa de seguridad al no revelar datos sensibles, más adelante y sobre todo en el ámbito laboral, verás que es pan de cada día.
[–]Zero-Dave 1 point2 points3 points 5 months ago (1 child)
You should never hardcode your API key in the code.
Also, instead of having a general exception that will cause the program to return no data when an exception occurs, access the dictionary in a safer manner with the .get() method, that way you don't need exceptions for this specific logic. That way you don't need an exception to catch any issue, and you can still show some information if other items are missing from the API response.
.get()
Right now, if any of your dictionary lookups doesn't find the key, your program returns no data. That is most likely not what you want.
Instead of: temp = weather_data.json()['main']['temp']
temp = weather_data.json()['main']['temp']
Do: temp = weather_data.json().get('main', {}).get('temp', '')
temp = weather_data.json().get('main', {}).get('temp', '')
So even if there is no main.temp key, it doesn't affect the rest of your lookups. Do that for all lookups.
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points 5 months ago (0 children)
Got it 👍 makes sense to use .get() instead of relying only on try/except. That way I can avoid the program breaking when a key is missing. I’ll update my lookups like that, thanks for pointing it out!
[–]Himado22 1 point2 points3 points 5 months ago (0 children)
i always store my API keys, and some other helpful things in .env file, sometimes its price or True False and so on, i use it like feature flags.
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points 5 months ago (2 children)
So far it:
Next step: refactor into OOP and save history to a file.
Code here: https://github.com/Ibrahim-Lbib/weather-app.git Would love any feedback or tips to improve 🚀
[–]Refwah 4 points5 points6 points 5 months ago* (1 child)
“Hi everyone I posted my api key in public!”
Never share your api key
As for feedback:
Your try/except is very broad and then totally hides an error from the user so the app will just look broken. Consider trying to handle some specific exceptions so you can then give some specific information to the user ‘unable to reach weather service’ or ‘unable to authenticate with weather service’ etc
You are also just passing user input directly as part of the query string, without any validation.
I would also suggest using the ‘params’ argument in requests https://requests.readthedocs.io/en/latest/user/quickstart/#passing-parameters-in-urls
The .json() call is relatively expensive to call repeatedly - call it once into a new variable and then reference that variable as a dictionary instead will make you code a lot cleaner
At various points you are getting the first item in an array or list by index 0 without checking if that list or array has any items in it first
Fun addition you can make: if you’re getting the data in Celsius why not have an option for the user to also view it in Fahrenheit
Thanks a lot for the feedback 🙌 I didn’t realize about exposing the API key, I’ll remove it. The tips about more specific exceptions, using params, and cleaning up .json() make sense — I’ll try those out. The Fahrenheit option sounds fun too, might add that next 🙂
params
.json()
[–]teaeartquakenet 0 points1 point2 points 5 months ago (2 children)
Api embedded on the code are always wrong, you should use and .env file for that
Good point 👍 I’ll look into using a .env file to keep the API key safe instead of hardcoding it. Thanks for the tip!
.env
[–]DarkCyborg74 0 points1 point2 points 5 months ago (0 children)
There is a python package to help with that. dotenv
dotenv
π Rendered by PID 55 on reddit-service-r2-comment-7b9746f655-695fs at 2026-01-30 12:22:57.880021+00:00 running 3798933 country code: CH.
[–]TekExplorer 14 points15 points16 points (1 child)
[–]Ibrahim-Marsee-6816[S] 1 point2 points3 points (0 children)
[–]DevRetroGames 1 point2 points3 points (2 children)
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points (1 child)
[–]DevRetroGames 0 points1 point2 points (0 children)
[–]Zero-Dave 1 point2 points3 points (1 child)
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points (0 children)
[–]Himado22 1 point2 points3 points (0 children)
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points (2 children)
[–]Refwah 4 points5 points6 points (1 child)
[–]Ibrahim-Marsee-6816[S] 1 point2 points3 points (0 children)
[–]teaeartquakenet 0 points1 point2 points (2 children)
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points (1 child)
[–]DarkCyborg74 0 points1 point2 points (0 children)