r/pythontips • • Jan 27 '22

Syntax First program with 170 lines of code.

This is the first time I wrote a "big" program. I began to learn python on 'n off about 2 weeks ago. What should/could I improve in my "writing".

>Code

27 Upvotes

7 comments sorted by

4

u/FancyASlurpie Jan 28 '22 edited Jan 29 '22
  • Use a logger, and then you won't need to leave commented out print statements (you can just adjust the log level to hide/show them as necessary).

  • Don't use pass inside except blocks like that, now its invisible as to something went wrong.

  • Use PEP8 consistent naming of functions (maybe in general just run this code through a library like Black

  • Avoid using globals, and instead pass the variables into the functions that need them, or create a class to hold the state.

  • The code around if uit is false/true, could be rewritten like this:

      for x in usernamesinalist:
        #print ("---",x,"---")                                           #for debugging
        if registration_enteredusername != x:
          uit = False
          pass
        if registration_enteredusername == x:
          uit = True
          break
      #print(uit)                                                          #for debugging
      if uit is False:
        logindaten[registration_enteredusername] = registration_enteredpassword
        registrationlabel.config(text="Sie sind registriert :)")
         registrationlabel.place(x=135, y=15)
      elif uit is True:
        registrationlabel.config(text = "Bitte benutzen sie ein anderen username!")
        registrationlabel.place(x=60, y=15)

becomes:

  for x in usernamesinalist:
     #print ("---",x,"---")
     if registration_enteredusername == x:
         print(f"Registration username {registration_enteredusername} matches username in list)
         registrationlabel.config(text = "Bitte benutzen sie ein anderen username!")
         registrationlabel.place(x=60, y=15)
         return

  logindaten[registration_enteredusername] = registration_enteredpassword
  registrationlabel.config(text="Sie sind registriert :)")
  registrationlabel.place(x=135, y=15)

I suspect the code in the password check which has a similar loop could be rewritten as well.

  • Rename the file to have a .py ending, that way you will get syntax highlighting on github

  • If you create a function you can reduce the repeated code in blocks like this:

      usernameentry = tk.Entry(root, fg="grey", borderwidth=0.5)
      usernameentry.place(x=120, y=55)
      usernameentry.insert(0, "username")
      usernameentry.bind("<FocusIn>", clearusernameentry)
    
      passwordentry = tk.Entry(root, fg="grey", borderwidth=0.5)
      passwordentry.place(x=120, y=85)
      passwordentry.insert(0, "password")
      passwordentry.bind("<FocusIn>", clearpasswordentry)    

could be written as:

    def create_entry_field(name, x_position, y_position, callback_function):
         entry_field = tk.Entry(root, fg="grey", borderwidth=0.5)
         entry_field.place(x=x_position,y=y_position)
         entry_field.insert(0, name)
         entry_field.bind("<FocusIn>", callback_function)
    
    user_name_entry = create_entry_field("username", 120, 55, clearusernameentry)
    password_entry = create_entry_field("password", 120, 85, clearpasswordentry)

This is useful as now you only need to edit a single place if you want to change the colour of your fields, or the width of borders etc. Making it easier to edit + maintain your code. If you end up needing more variety in terms of the Tk.Entrys you could pass in an object which contains the settings you want it to create (rather than having a very long list of parameters on the function).

Overall really good start, keep at it and take the time to look back at your code and think how you can improve it in terms of readability, and maintainability.

1

u/[deleted] Jan 28 '22

So "if registration_enteredusername:" is the same as "if registration_enteredusername == x:" ?

1

u/FancyASlurpie Jan 28 '22

Ah no you should change that line, it was quite late and I probably confused it with the Boolean assignment you were doing below

1

u/[deleted] Jan 28 '22

ahh ok

12

u/ziaaron Jan 27 '22

If have a few suggestions:

  • use if tuit instead of if tuit == True

  • drop off the pass statements if you have some code in an if block

  • use the else statement if you two complementary conditions

  • use consistent formatting (e.g. the same number of newlines after each method)

  • avoid using the global keyword

  • don't use hardcoded values in multiple places (like 175 in your code, and use something like BLACK = "black" at the beginning of the file or better use Enums)

  • in an try/except block, be specific about the exceptions

  • don't use variable names in german (logindaten), just stick with english

  • use self explaining variable names

For your first two weeks that's pretty good, just keep learning.

2

u/dw00600 Jan 27 '22

A little more internal doc is always a good thing!

3

u/[deleted] Jan 27 '22

Nice! Few comments:

  • better file name

  • put all the functions into a class

  • make a new main.py file that instantiates and runs the class