patternpythonMinor
Password generator script
Viewed 0 times
scriptgeneratorpassword
Problem
What I was trying to do here was create a Python script that would generate passwords with 16 characters with uppercase and lowercase letters, and numbers. The generated password would then write itself to a file.
Let me know what you think because I know they say the point of Python is to be able to write powerful scripts that can do what other languages can do in a few lines. I feel like this is probably way more then it should be for what it's doing.
```
import random
import string
print "Welcome to PassBook!"
def genPasswd():
global route
global password
global username
global account
global entry
account = raw_input("Account: ")
username = raw_input("Username: ")
key1 = random.randrange(10,100)
key2 = random.randrange(10,100)
key3 = random.randrange(10,100)
key4 = random.randrange(10,100)
keya = str(random.choice(string.letters) + random.choice(string.letters))
keyb = str(random.choice(string.letters) + random.choice(string.letters))
keyc = str(random.choice(string.letters) + random.choice(string.letters))
keyd = str(random.choice(string.letters) + random.choice(string.letters))
key1a = str(key1) + str(keya)
key2b = str(key2) + str(keyb)
key3c = str(key3) + str(keyc)
key4d = str(key4) + str(keyd)
password = str(key1a + key2b + key3c + key4d)
entry = "Account: " + account + " - Username: " + username + " - Password: " + password
file = open("/home/meta/Desktop/Python2/passbook/passbook.txt", "a")
file.write("\n" + entry + "\n")
print entry
return key1
return key2
return key3
return key4
return keya
return keyb
return keyc
return keyd
return key1a
return key2b
return key3c
return key4d
return password
return username
return account
return entry
def queryPb():
search = raw_input("For which account are you searching: ")
read = open("/home/meta/Desktop/Python2/passbook/passbook.txt", "r")
for
Let me know what you think because I know they say the point of Python is to be able to write powerful scripts that can do what other languages can do in a few lines. I feel like this is probably way more then it should be for what it's doing.
```
import random
import string
print "Welcome to PassBook!"
def genPasswd():
global route
global password
global username
global account
global entry
account = raw_input("Account: ")
username = raw_input("Username: ")
key1 = random.randrange(10,100)
key2 = random.randrange(10,100)
key3 = random.randrange(10,100)
key4 = random.randrange(10,100)
keya = str(random.choice(string.letters) + random.choice(string.letters))
keyb = str(random.choice(string.letters) + random.choice(string.letters))
keyc = str(random.choice(string.letters) + random.choice(string.letters))
keyd = str(random.choice(string.letters) + random.choice(string.letters))
key1a = str(key1) + str(keya)
key2b = str(key2) + str(keyb)
key3c = str(key3) + str(keyc)
key4d = str(key4) + str(keyd)
password = str(key1a + key2b + key3c + key4d)
entry = "Account: " + account + " - Username: " + username + " - Password: " + password
file = open("/home/meta/Desktop/Python2/passbook/passbook.txt", "a")
file.write("\n" + entry + "\n")
print entry
return key1
return key2
return key3
return key4
return keya
return keyb
return keyc
return keyd
return key1a
return key2b
return key3c
return key4d
return password
return username
return account
return entry
def queryPb():
search = raw_input("For which account are you searching: ")
read = open("/home/meta/Desktop/Python2/passbook/passbook.txt", "r")
for
Solution
Your sentiment is indeed correct, this code is much longer than it needs to be and can be shortened 10x while retaining the same functionality.
You are doing 4 things in a single function!
A function should do 1 thing only.
Let's say we want to generate a password:
This function is shorter than yours not only because it does not care about Input/Output, but also because it uses loops (Generator expressions here) to do the same thing many times.
You may call this function in a user-interface that handles input and output like:
Of course you may want to make the interface nicer or make it handle files, but the backbone is here.
You are doing 4 things in a single function!
- Asking for input
- Generating a password
- Printing
- Writing to a file.
A function should do 1 thing only.
Let's say we want to generate a password:
def generate_password():
numbers = (str(random.randint(10, 99)) for _ in range(4))
letters = (random.choice(string.letters) for _ in range(4))
return ''.join(numbers) + ''.join(letters)This function is shorter than yours not only because it does not care about Input/Output, but also because it uses loops (Generator expressions here) to do the same thing many times.
You may call this function in a user-interface that handles input and output like:
print(raw_input("Username ") + " " + raw_input("Surname ") + ": " + generate_password())Of course you may want to make the interface nicer or make it handle files, but the backbone is here.
Code Snippets
def generate_password():
numbers = (str(random.randint(10, 99)) for _ in range(4))
letters = (random.choice(string.letters) for _ in range(4))
return ''.join(numbers) + ''.join(letters)print(raw_input("Username ") + " " + raw_input("Surname ") + ": " + generate_password())Context
StackExchange Code Review Q#107714, answer score: 7
Revisions (0)
No revisions yet.