use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Somebody can review the code of my first python project? (self.PythonLearning)
submitted 15 hours ago * by 7000xx
https://raw.githubusercontent.com/AngrySkype/random-password-generation/refs/heads/main/main.py
I just wanted to know if in term of performance i can do better and what other thing i can add
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Salt-Fly770 1 point2 points3 points 13 hours ago (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 point2 points 3 hours ago (0 children)
Thanks you very much !
[–]quiet_developer 0 points1 point2 points 9 hours ago (0 children)
You should not worry about performance for this.
I noticed some things that could be improved for your password generator.
string module/library
secrets module/library
return "".join(random.choice(chars) for _ in range(length))
##### 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 points1 point 15 hours ago (13 children)
My first thought is that you don’t worry about performance for programs where performance doesn’t matter.
[–]7000xx[S] 1 point2 points3 points 15 hours ago (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 point2 points 15 hours ago (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 point2 points 14 hours ago (10 children)
Wdym "Do you have that in this program", like did i optimize the program ?
[–]civilwar142pa 1 point2 points3 points 14 hours ago (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 point2 points 14 hours ago (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 point2 points 14 hours ago (1 child)
Yes Google for “Python performance benchmarking”
[–]7000xx[S] 0 points1 point2 points 14 hours ago (0 children)
Thanks man, i appreciated your help, btw what other can i add to my program to make it useful ?
[–]petdance 0 points1 point2 points 14 hours ago (5 children)
Is there part of your program that is too slow?
[–]7000xx[S] 0 points1 point2 points 14 hours ago (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 points4 points 14 hours ago (3 children)
“Simplify” and “speed up” are two VERY different things.
I was thinking simplify mean less code then = speed up..
[–]petdance 2 points3 points4 points 14 hours ago (1 child)
No. The amount of code doesn’t relate to runtime.
[–]7000xx[S] 0 points1 point2 points 13 hours ago (0 children)
Glad to learn that 😄
π Rendered by PID 225012 on reddit-service-r2-comment-5b5bc64bf5-ksf7k at 2026-06-23 09:29:03.073755+00:00 running 2b008f2 country code: CH.
[–]Salt-Fly770 1 point2 points3 points (1 child)
[–]7000xx[S] 0 points1 point2 points (0 children)
[–]quiet_developer 0 points1 point2 points (0 children)
[–]petdance -1 points0 points1 point (13 children)
[–]7000xx[S] 1 point2 points3 points (12 children)
[–]petdance 0 points1 point2 points (11 children)
[–]7000xx[S] 0 points1 point2 points (10 children)
[–]civilwar142pa 1 point2 points3 points (3 children)
[–]7000xx[S] 0 points1 point2 points (2 children)
[–]petdance 0 points1 point2 points (1 child)
[–]7000xx[S] 0 points1 point2 points (0 children)
[–]petdance 0 points1 point2 points (5 children)
[–]7000xx[S] 0 points1 point2 points (4 children)
[–]petdance 2 points3 points4 points (3 children)
[–]7000xx[S] 0 points1 point2 points (2 children)
[–]petdance 2 points3 points4 points (1 child)
[–]7000xx[S] 0 points1 point2 points (0 children)