this post was submitted on 25 Apr 2025
18 points (87.5% liked)

Python

7045 readers
46 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
18
How is my Python code? (raw.githubusercontent.com)
submitted 18 hours ago* (last edited 18 hours ago) by the_citizen@lemmy.world to c/python@programming.dev
 

I don't know if it's the true place to ask, apologizing if not. I started to python one and half week ago. So I'm still beginner.

I made a terminal based weather application with python. What do you think about the code, is it good enough? I mean is it professional enough and how can I make the same functions with more less code?

Here's the main file (I also added it as url to post): https://raw.githubusercontent.com/TheCitizenOne/openweather/refs/heads/main/openweather.py
Here's the config.json file: https://raw.githubusercontent.com/TheCitizenOne/openweather/refs/heads/main/config.json

you are viewing a single comment's thread
view the rest of the comments
[–] solrize@lemmy.world 9 points 15 hours ago (1 children)

It's mostly ok, my immediate reaction is that it's unnecessary to put the whole program into a class. Python isn't Java. It's fine to write procedures at the top level.

Also as someone said, consider using enums, and also the new case statement as of Python 3.10. Type annotations also can make things clearer. I've been using mypy to check mine but there are some alternatives now that might be better.

There is not much error checking but that can be ok if you are the only user.

If the config file might be crafted maliciously, it can use escape codes for a terminal playback attack: https://www.cyberark.com/resources/threat-research-blog/dont-trust-this-title-abusing-terminal-emulators-with-ansi-escape-characters

Be careful of that.

[–] the_citizen@lemmy.world 1 points 6 hours ago (1 children)

Yes I realized that I put everything to two classes. It's too much. I'll make more classes for more attributes.

I didn't know enums until now and it's awesome. I will do some exercises about it.

Last thing is interesting. I need to read that. Thank you for suggestions.

[–] solrize@lemmy.world 2 points 1 hour ago

There's no need to make more classes. You can move some of the code so it's not in any class.