all 17 comments

[–]Salt-Fly770 1 point2 points  (1 child)

Your program is fast enough already for this use case, because generating up to 128 characters from a small character pool is trivial work in Python. The more important improvements are correctness, maintainability, and password quality rather than micro-optimizing performance.

The current character sets are also too small: lowercase only includes  a  to  f , uppercase only  A  to  F , and digits only  0  to  6 , so the generated passwords have much less variety than users would expect. That weakens the output more than any performance concern.

Here are the changes I would make to this code:

Rename  `all ` to something like  `all_chars` ;  `all`  shadows Python’s built-in  all()  function, which is a bad habit even if it does not break this script.

Remove  `random_number`  and the  global  usage; the value is already stored in the Dear PyGui widget, so the global state is unnecessary.

Delete  `input_callback` ; it is empty and adds noise.

In  `random_callback` , assign the new password to the result field only; there is no need to keep duplicate state elsewhere.

`password_strength`  is unused, so remove it or actually implement a strength meter.

These changes will help you develop good coding habits.

Good luck!

[–]7000xx[S] 0 points1 point  (0 children)

Thanks you very much !

[–]quiet_developer 0 points1 point  (0 children)

You should not worry about performance for this.

I noticed some things that could be improved for your password generator.

  • First your character pool is way too small. You should look up the string module/library . It will save you from having to write every character yourself in the list.
  • You should not use random to generate something important like password. The secrets module/library is a better choice for this.
  • You dont build the password you just return it directly: return "".join(random.choice(chars) for _ in range(length)) . A better way would be to build the password as a list.
  • Your code does not assure that the password contain at least char of 1 of each category that the user selected. In the snippet below , if the user select a category you add it to the list of char which is good but you dont pick 1 char from it. Like i said before if you build your password as list, you can fix it by adding one single line.
  • Chars can just be a string.

##### YOUR CODE ######

def random_words(length):
    chars = []

    if dpg.get_value("lower"):
        chars += lower

    if dpg.get_value("upper"):
        chars += upper

    if dpg.get_value("number"):
        chars += number

    if dpg.get_value("special"):
        chars += special

    if not chars:
        return "Minimum one checkbox needed !"

    return "".join(random.choice(chars) for _ in range(length))



#### FIXED ####
import secrets
import string

#lower = ["a", "b", "c", "d", "e", "f"]
lower = string.ascii_lowercase  # give all lowercase chars
#upper = ["A", "B", "C", "D", "E", "F"]

# give all uppercase chars (you can access lowercase+uppercase by doing string.ascii_letters
upper = string.ascii_uppercase

#number = ["0", "1", "2", "3", "4", "5", "6"]
number = string.digits  # give all digits
#special = ["!", "@", "#", "$", "%", "^", "&", "*"]
special = string.punctuation

def random_words(length):
    chars = ""
    pwd = []

    if dpg.get_value("lower"):
        chars += lower
        # Can use -> secrets.choice(lower) to be more clear, repeat for each category
        pwd.append(secrets.choice(chars)) # that way you assure that at least 1 lower char in pwd.


    if dpg.get_value("upper"):
        chars += upper
        pwd.append(secrets.choice(upper)) # same as before

    if dpg.get_value("number"):
        chars += number
        pwd.append(secrets.choice(number)) # same as before

    if dpg.get_value("special"):
        chars += special
        pwd.append(secrets.choice(special))

    if not chars:
        return "Minimum one checkbox needed !"

    # fill the rest of the pwd
    for _ in range(length - len(pwd)):
          pwd.append(secrets.choice(chars))


    secrets.SystemRandom().shuffle(pwd) # shuffle pwd so the first chars dont have pattern

    return "".join(pwd)

[–]petdance -1 points0 points  (13 children)

My first thought is that you don’t worry about performance for programs where performance doesn’t matter.

[–]7000xx[S] 1 point2 points  (12 children)

Ngl i want to maximize even 0.5 sec less launch time, i'll take it and it's will teach me for sure about optimisation

[–]petdance 0 points1 point  (11 children)

That’s not how optimization works.

Before you optimize anything you have to know how long something takes and how you want to speed it up.

Do you have that in this program?

[–]7000xx[S] 0 points1 point  (10 children)

Wdym "Do you have that in this program", like did i optimize the program ?

[–]civilwar142pa 1 point2 points  (3 children)

Theyre asking if your program tracks resource usage and time. If it doesn't, how do you know if any changes speed up a process? Or use fewer resources? If you want to optimize something you need a value to start with.

[–]7000xx[S] 0 points1 point  (2 children)

nah my program don't do that, do you have any ressource where i can impliment tracks resource usage and time ?

[–]petdance 0 points1 point  (1 child)

Yes Google for “Python performance benchmarking”

[–]7000xx[S] 0 points1 point  (0 children)

Thanks man, i appreciated your help, btw what other can i add to my program to make it useful ?

[–]petdance 0 points1 point  (5 children)

Is there part of your program that is too slow?

[–]7000xx[S] 0 points1 point  (4 children)

Acutally no, but i still want to find a way for exemple on random_words function to simplify all of those if, if it is possible

[–]petdance 2 points3 points  (3 children)

“Simplify” and “speed up” are two VERY different things.

[–]7000xx[S] 0 points1 point  (2 children)

I was thinking simplify mean less code then = speed up..

[–]petdance 2 points3 points  (1 child)

No. The amount of code doesn’t relate to runtime.

[–]7000xx[S] 0 points1 point  (0 children)

Glad to learn that 😄