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

  • solrize@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    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.

    • citizen@lemmy.worldOP
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      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.

  • FizzyOrange@programming.dev
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    2 months ago

    Pretty good for a week and a half! I would recommend setting up a project using uv and also adding type hints and check them with Pyright (NOT Mypy).

  • KindaABigDyl@programming.dev
    cake
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    You appear to be afraid of spaces. Everything is cramped together.

    Just bc you don’t have to put a space after colons or after equals and commas and whatnot doesn’t mean you shouldn’t

    Don’t be afraid of spaces.

    They make it easier for you to read your code when you come back later

    • citizen@lemmy.worldOP
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      I revised the code. Added some spaces and comments for better readability. Hope it’s better now.

      • FizzyOrange@programming.dev
        link
        fedilink
        arrow-up
        2
        ·
        2 months ago

        Instead you should just use an autoformatter. Black is good. Ruff is probably good too but I don’t have a lot of experience with it. YAPF is not good; don’t use it.

      • taaz@biglemmowski.win
        link
        fedilink
        English
        arrow-up
        0
        arrow-down
        1
        ·
        edit-2
        2 months ago

        Usually, you would use a formatter anyway - it’s good to know the standard way but for day to day coding I just have a shortcut bound that runs ruff format (you can even have it done automatically on file save).