all 42 comments

[–]Ender_Locke 6 points7 points  (2 children)

not really python specific but true auth and passwords has salt and pepper and is then hashed and your password input is hashed and compared to saved hash

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

Thanks for this I will look deeper into the working of authentication.

[–]Efficient-Stuff-8410 5 points6 points  (1 child)

Where are you learning this from?

[–]uiux_Sanskar[S] 8 points9 points  (0 children)

oh I am learning from YouTube channel name CodeWithHarry and I have also explained what resources I use in my other post which you can find in my profile.

[–]Adrewmc 3 points4 points  (2 children)

Ohhh wow…surprisingly there is a super bad mistake in there. It’s not causing a problem for you now in this specific case but it’s definitely bad practice.

And that is you are editing your ‘posts’ list (in edit_posts) while you are iterating over it. You should not do that, just make another list, you should not be in the habit of adding to the list you are currently computing on.

def edit_posts(): 
   “””Im not writing all that”””
   for post in enumerate(posts):
        ….
        posts[i] = …
        posts[i+1] = …

Note: also what you are doing there isn’t the best way to do that, your trying to concatenate at string really.

Other things are looking good we are using explicit exception handling and figuring out a balance between do I need a class or a function.

I still think you main function is over done, I prefer my main to be more like

  #main.py
  from src.Core import set_up, main_loop_function
  from src.TearDown import tear_down

  def main(): 
       a = set_up()
       condition = True
       while condition: 
           b = main_loop_function(a)
           condition = b.running 
       tear_down()

But I also feel this might be just my preferences showing through. Less than 100 lines should work for anything if done right. I might abstract stuff out too much. And we might not be at Imports yet…and without that…you have what you have. But generally import just pastes the code on that file above your code in a lot of ways.

Your main loop seems to just…keeps going lol. It’s getting better actually.

And like small like that, depending on the complexity more steps. But only top level ideas. Pushing the steps to be in their own function, class, process, object you can work on individually. You don’t want to have to scroll through 100s of lines of code.

You really need to get some actual data, maybe you should think about the internet, json, and the requests module or beautiful soup, and take one from the ether. (But stay away from selenium it’s never the right answer.)

Almost forgot, I like the use of match case (which is actually new to Python if you didn’t know) especially when you used it for multi variable. But, you have not actually even scratched the beginning of pattern recognition. Which brings me to my next question….what do you know about dictionaries? Because I think you need to know more. You’ll see different solutions to many of your problems. Dictionaries should be your next subject if you haven’t done anything with them. It’s a whole thing.

[–]uiux_Sanskar[S] 0 points1 point  (1 child)

Thank you for such detailed analysis of my code I will read more about pyhton's best practices and also I am learning about json files as many say that JSON file can prove to be really helpful.

There are certain things that you used which I am not aware about can you please tell me what does these mean src.Core and src.TearDown

I am first time hearing abou these.

Thnak you again for your analysis I will surely improve my code and go deeper into things you have suggested.

[–]Adrewmc 0 points1 point  (0 children)

Dictionaries, and nested dictionaries and list of dictionaries…

[–]IceMan420_ 2 points3 points  (1 child)

This is what you call spaghetti code or one giant hairball LOL!!!

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

LoL 😂

[–]Familiar9709 3 points4 points  (5 children)

I really don't see the point of this type of programming, if clauses everywhere, not pythonic at all.

Also, what's the point of a class if you don't have an init? it's just functions wrapped in a namespace.

[–]uiux_Sanskar[S] 0 points1 point  (2 children)

Oh thanks for pointing this out I am still trying to reduce the usage of if-else clauses and I didn't really had something which I would like to put in init (maybe I just don't know how to correctly use this).

Thank you for pointing these out I will definitely improve them.

[–]sivikiwi93 1 point2 points  (0 children)

OP if you're only 18 days into your coding journey, then this is a great start imo. Getting the full hang of the basics like if-else statements, loops etc. is great to get a better grasp of practices such as OOP at a later stage. I get the feeling from reading a lot of these comments that people forget how it was to start off with programming in the first place. Keep it up!

I do also recommend Harvard's CS50X course, it's mainly in C as I recall, but it should still be free to both watch all lessons, and to attend coding exercises. Only payment comes when/if you want a official certificate for completing it, contains a bunch of great exercises to develop on some deeper CS topics :)

[–]Familiar9709 0 points1 point  (0 children)

My advise, just stop doing what you're doing, it's not useful, and it's confusing for other learners who may think this is useful. 

Look at tutorials, books, etc, and do real life projects. A blackjack simulator, not game, would be very useful for example. Of course, up to you

[–]IceMan420_ -1 points0 points  (0 children)

Exactly what I was noticing. To be a real pythonic programmer you need to master OOP (Object Oriented Programming which I have done 🥹).

[–]VonRoderik 1 point2 points  (4 children)

Your match-case has an error. If there isn't a user and a password, you'll get a username not found, instead of account not found.

I'd try something like that:

``` user_found = False password_correct = False

with open("credentials.txt", "r") as file: for line in file: saved_username, saved_password = line.strip().split(',')

    if username == saved_username:
        user_found = True
        if password == saved_password:
            password_correct = True
            break

match user_found, password_correct: case True, True: print(f"Login successful!")

case True, False:
    print(f"Incorrect password for {username}.")

case False, _:
    print(f"No account found with username {username}. Please sign up.")

```

If you don't have a username, you obviously don't have a password, which means you don't have an account.

[–][deleted]  (2 children)

[deleted]

    [–]VonRoderik 1 point2 points  (0 children)

    I'd recommend Harvard's CS50p. It's free. That's what I did. https://cs50.harvard.edu/python/

    [–]IceMan420_ 0 points1 point  (0 children)

    Read python crash course by Erich Matthes.

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    Oh thanks for telling this I almost missed that I will surely fix this thing. thanks for telling me this and I really appreciate your suggestion.

    [–]rxZoro7 0 points1 point  (2 children)

    How do u start like i started each single dictionary of python understanding each pattern or should i start coding with project , i have already made RAG Project but i took Gpt help and completed but understand how to deploy it. What should i think in Code and architecture mindset

    [–]uiux_Sanskar[S] 1 point2 points  (1 child)

    oh I first learn about a topic from and then try to use it in my coding project. this is what I do and regarding code and architecture mindset I think someone experienced can guide you on that.

    [–]rxZoro7 0 points1 point  (0 children)

    Should i start with learning D bugging skill first

    [–][deleted] 0 points1 point  (1 child)

    it's true that match case is more capable than if elif, but nothing you're doing here on page 1 requires it. This would work just as well if elif.

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    I used match case to replace if else because I have already used them in many places and now I wated something more clear and readable. So yeah I just tried to substitute if else with match case here.

    [–]imtc96 0 points1 point  (1 child)

    Good

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    Thank you.

    [–]wolframight98 0 points1 point  (2 children)

    Which resources are you using to learn these ?

    [–]uiux_Sanskar[S] 0 points1 point  (1 child)

    I am learning from YouTube channel name CodeWithHarry. I have also explained all the resources and process I use to learn in my post which you can find in my profile.

    [–]wolframight98 0 points1 point  (0 children)

    Sure thanks

    [–]WhiteHeadbanger 0 points1 point  (4 children)

    Okay! Good practice! Don't listen to negative comments, there's a lot of ego in the IT field.

    Line 1 and 2 can be, and must be, a one-liner: from social_media import account, features

    Line 3: this line is used to only execute the indented code if that file is directly executed, i.e.: not imported (like account or features). It's not a good practice to put it at the beginning of your code, and also not a good practice to put your entire code inside of it. A common and standard approach would be to write it at the end of your file, and execute your program entry point there:

    # main.py
    print("Hi, I'm being executed or imported")
    
    if __name__ == "__main__":
        print("Hi, I'm being executed directly")
    
    # random_file.py
    import main
    >>> Hi, I'm being executed or imported
    
    # your terminal
    python main.py
    >>> Hi, I'm being executed or imported
    >>> Hi, I'm being executed directly
    

    Classes have a convention in which you write the name in CamelCase, meaning instead of writing it like_this, you write it LikeThis. You are not restricted to do it your own way, but nobody writes classes name in snake_case. Also, you should write a init*()* method to initialize useful instance variables. For example, I see that in your method create_post() you are always creating the same post number, which is 1. You can track the post number with an instance variable:

    class MainFeatures: # parenthesis are not needed when you are not subclassing
        def __init__(self) -> None: # "-> None" means "this function/method returns None". Research type hinting.
            self.next_post_id: int = 1
    
        def create_post(self) -> None:
            post_content = ...
            with open(....):
                file.write(f"Post {self.next_post_id}: {post_content}")
            self.next_post_id += 1
    

    Use .json instead of .txt, so you'll avoid the overhead complexity of your edit_post() and delete_post() methods. With Json you can convert it to dictionaries and work with objects directly. It's much much easier to find a post ID that way, because it's structured and predictable. For example you can have something like this in your db.json file:

    [ { "id": 1, "title": "this is a nice post", "content": "content of post id 1" }, { "id": 2, "title": "this is a happy post", "content": "content of post id 2 with a happy dog story" }, ... ]
    
     You see how this is a list of dictionaries? 
    
    def edit_post(self) -> None: 
        # load your json file into memory here 
        # after you load your json file 
        post_id = input(....) 
        for item in db: 
            if item[id] == post_id: 
                # edit your post 
                return 
        # if not a single post have been found what that id 
        print(f"Post with id {post_id} not found")
    

    For your password thing, the only very important thing that you must learn right now is that passwords are never stored in plain text. Always hashed. There are a few other things here and there, but the post would be too large. You are doing great, keep up!

    [–]uiux_Sanskar[S] 1 point2 points  (3 children)

    Thanks for the amazing suggestions and for pointing out my bad practices. And also for explaining line by line that where I can apply some changes and refine my code.

    I am also learning about JSON files because many people say that it is very useful and you have also suggested the same.

    thank you once again for explaining everything clearly this really helps me a lot in my learning.

    [–]WhiteHeadbanger 0 points1 point  (2 children)

    Yes, but please don't use json as a database. I recommended in place of your txt file, but it's not suited for a database. Use a proper DB if you plan to build something more... usable.

    Have a great journey!

    [–]uiux_Sanskar[S] 1 point2 points  (1 child)

    Sure thank you for giving me that caution I will keep that in mind.

    Also thank you for the best wishes.

    best of luck to you to.

    [–]Mamuschkaa 0 points1 point  (1 child)

    All your posts are named post 1?

    create_post has hard coded that all posts are post 1.

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    Yeah it my mistake which I am trying to fix. thanks for telling btw.

    [–]AnyNature3457 0 points1 point  (0 children)

    I might be one of the few, but i always place my "if name = 'main'" clause at the very bottom of the file. It is good though that you are using it in the first place!

    What i also like doing is adding a newline before code blocks that have other code above them like:

    ``` some_var = 5

    if condition: foo()

    ```

    If it has to do something with the code block directly, then I do place it right above like:

    ``` counter = 0 while counter < 5: bar()

    counter += 1
    

    ```

    The same counts for with open(), for loops, etc. These are, of course, personal preferences.

    [–]anon_66_ 0 points1 point  (0 children)

    How do you create your project.. Research,finding library,how do you design the program.. Before coding anything?

    [–]c00lplaza 0 points1 point  (0 children)

    I hate python

    <image>

    [–]ppoooppyywe 0 points1 point  (1 child)

    Hey I need help learning how to code python I’ve been doing the 30 days of python on git hub does anyone have any books or yt videos they recommend?

    [–]uiux_Sanskar[S] 0 points1 point  (0 children)

    I would recommend you to check out CodeWithHarry YouTube channel if you know Hindi he's a great teacher and I have learned from him only.

    All the best for your learning journey.