chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Password Generator filter_list
Author
Message
Password Generator #1
Imports

For this project we will need "os", "random", and "string".

Code:
import os
import random
import string

Definitions

You only need to define one thing for this project. The Close event.

Code:
def close(event):
    os.system('taskkill /PID %d /F' % os.getpid())

Generator

Code:
length = random.randint(8,30)
chars = string.ascii_letters + string.digits + '~!@#$%^&*()_+'
random.seed = (os.urandom(1024))

Printing and re-generating

All we need to do now is print the password and prompt the user if they want a new one.

Code:
password = print("[PASSGEN]Generating password.\n[PASSGEN]Password generated:\n" + " ".join(random.choice(chars) for i in range(length)).replace(" ",""))
answer = input("[PASSGEN]Would you like to generate a new password? ")
if answer == "yes":
    os.startfile("password generator.py"),close(password)
elif answer == "no":
    close(password)

It's very short and simple.

Code:
import os
import random
import string
import time

def close(event):
    os.system('taskkill /PID %d /F' % os.getpid())

length = random.randint(8,30)
chars = string.ascii_letters + string.digits + '~!@#$%^&*()_+'
random.seed = (os.urandom(1024))

password = print("[PASSGEN]Generating password.\n[PASSGEN]Password generated:\n" + " ".join(random.choice(chars) for i in range(length)).replace(" ",""))
answer = input("[PASSGEN]Would you like to generate a new password? ")
if answer == "yes":
    os.startfile("password generator.py"),close(password)
elif answer == "no":
    close(password)

Hope you liked the generator I coded. I think I am getting better everytime, especially being a Python noob.

Reply

RE: Password Generator #2
Thanks for the release! Hope you had fun making it. I mostly just use an opensource one from sourceforge though.

Reply

RE: Password Generator #3
(01-26-2014, 09:58 AM)Sparks Wrote: Thanks for the release! Hope you had fun making it. I mostly just use an opensource one from sourceforge though.

Course I had fun. Tongue I love programming. Even though I partially suck at it. xD

Reply

RE: Password Generator #4
(01-26-2014, 09:59 AM)Duubz Wrote: Course I had fun. Tongue I love programming. Even though I partially suck at it. xD

Lol same here Tongue

Reply

RE: Password Generator #5
TL;DR:


Reply

RE: Password Generator #6
Why are you using os functions to open/close? Just recursively call a function with all the generation code in it if the user wants another password.

Instead of random.choice(chars) do

Code:
chr(random.randint(94) + 32 )

It is faster, uses less code, and looks neater.

(94 printable english characters in ascii, they start at 32)
micrictor@creep.im

Reply

RE: Password Generator #7
I remember making one of these back a year...
It was simpler I think... A lot simpler. And n00bish.
However, it also took in user input for the length (or you could enter random, in which case the length was random)

anyway, this is nice... And in ways better than mine.

however, isn't and "else" option required in the if statement in the end?
(maybe that's just my habit though... if something goes wrong, then i give and "ERROR" message xD)
[Image: TeusoI9.png]
BACK UNDER YOUR BEDS
TRY TO GET A GOOD NIGHT'S SLEEP NOW

Reply

RE: Password Generator #8
You are indeed getting better! But there are a few things I really can't overlook;

Critical:
The script requires that it be saved with the name "password generator.py", because of this code:
Code:
if answer == "yes":
    os.startfile("password generator.py")
    close(password)
You could name it different things if it were instead done like this:
Code:
answer = 'yes'
while answer == 'yes':
    length = random.randit(8,30)
    ...
    answer = input("[PASSGEN] Would you like to generate a new password?")
close(password)
The second is that it relies on being run on a windows machine, with the line
Code:
os.system('taskkill /PID %d /F' % os.getpid())
Other os's have different kill commands, a way that will work no mater what os it's on would be
Code:
exit()
Which will ALWAYS exit the python interpreter.

Bad habits:
It's good practice to only import the classes you need from a module EX:
Code:
from os import urandom, startfile, system, getpid
This is so the python interpreter ONLY loads these classes and nothing more, it's just an optimization thing.
[Image: jWSyE88.png]

Reply

RE: Password Generator #9
This looks pretty decent.
Well done OP.
[Image: helper.png]


Reply

RE: Password Generator #10
(01-26-2014, 08:19 PM)cr33pyguy Wrote: I remember making one of these back a year...
It was simpler I think... A lot simpler. And n00bish.
However, it also took in user input for the length (or you could enter random, in which case the length was random)

anyway, this is nice... And in ways better than mine.

however, isn't and "else" option required in the if statement in the end?
(maybe that's just my habit though... if something goes wrong, then i give and "ERROR" message xD)

"elif" means else-if. I'm pretty sure it can function without an "else" but will shutdown if there is neither "yes" nor "no" in the input.

(01-26-2014, 09:02 PM)3SidedSquare Wrote: You are indeed getting better! But there are a few things I really can't overlook;

Critical:
The script requires that it be saved with the name "password generator.py", because of this code:
Code:
if answer == "yes":
    os.startfile("password generator.py")
    close(password)
You could name it different things if it were instead done like this:
Code:
answer = 'yes'
while answer == 'yes':
    length = random.randit(8,30)
    ...
    answer = input("[PASSGEN] Would you like to generate a new password?")
close(password)
The second is that it relies on being run on a windows machine, with the line
Code:
os.system('taskkill /PID %d /F' % os.getpid())
Other os's have different kill commands, a way that will work no mater what os it's on would be
Code:
exit()
Which will ALWAYS exit the python interpreter.

Bad habits:
It's good practice to only import the classes you need from a module EX:
Code:
from os import urandom, startfile, system, getpid
This is so the python interpreter ONLY loads these classes and nothing more, it's just an optimization thing.

Thanks for the advice. However I run a Windows operating system and I don't really know much about other operating systems that run Python because it's different on Linux than it is on Windows. The exit is pretty useful, thanks for that. I seem to be putting useless lines of code that are useful yet can be done better.
(This post was last modified: 01-27-2014, 01:25 AM by Stocking.)

Reply






Users browsing this thread: 1 Guest(s)