![]() |
|
Generate possible usernames - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Generate possible usernames (/Thread-Generate-possible-usernames--49715) Pages:
1
2
|
Generate possible usernames - RogueCoder - 05-16-2013 This script generates possible username combinations based on whatever you feed the script with, but probably most optimal for real names. That's at least what the intentional use was It can take a single name and a file with one name per line./username.py John Doe ./username.py -f names.txt (Not restricted to .txt) PS: It's my very first time writing code in Python so please feel free to suggest how to improve, optimize and clean the code ![]() Code: #!/usr/bin/python
import sys
def makeUsernames(names):
result = []
for name in names:
name = name.replace("\n", '')
name = name.lower()
parts = name.split(' ')
fname = parts[0]
if len(parts) > 1:
lname = parts[len(parts) - 1]
result.append(fname + lname)
result.append(lname + fname)
result.append(fname[0] + lname)
result.append(fname + lname[0])
result.append(lname[0] + fname)
result.append(lname + fname[0])
result.append(fname + '.' + lname)
result.append(lname + '.' + fname)
result.append(fname[0] + '.' + lname)
result.append(lname[0] + '.' + fname)
result.append(fname + '_' + lname)
result.append(lname + '_' + fname)
result.append(fname[0] + '_' + lname)
result.append(lname[0] + '_' + fname)
result.append(fname + '-' + lname)
result.append(lname + '-' + fname)
result.append(fname[0] + '-' + lname)
result.append(lname[0] + '-' + fname)
else:
result.append(fname)
return result
names = []
if len(sys.argv) < 2:
sys.exit('Syntax error: Not enough arguments')
elif sys.argv[1] == '-f':
if len(sys.argv) != 3:
sys.exit('Syntax error: You must supply an existing file')
else:
filename = sys.argv[2]
try:
with open(filename) as f:
names = f.readlines()
except IOError:
sys.exit('IOError: No such file or directory')
else:
names = [' '.join(sys.argv[1:])]
if len(names) > 0:
result = makeUsernames(names)
for name in result:
print name
else:
sys.exit('Unable to generate data')Some example outputs Spoiler: Single name "Jane Doe"Code: janedoe
doejane
jdoe
janed
djane
doej
jane.doe
doe.jane
j.doe
d.jane
jane_doe
doe_jane
j_doe
d_jane
jane-doe
doe-jane
j-doe
d-janeSpoiler: File with the names "John Doe" and "Eric Smith"Code: johndoe
doejohn
jdoe
johnd
djohn
doej
john.doe
doe.john
j.doe
d.john
john_doe
doe_john
j_doe
d_john
john-doe
doe-john
j-doe
d-john
ericsmith
smitheric
esmith
erics
seric
smithe
eric.smith
smith.eric
e.smith
s.eric
eric_smith
smith_eric
e_smith
s_eric
eric-smith
smith-eric
e-smith
s-ericUpdate: * Added sys.exit() on errors * The makeUsersnames() function now returns the value instead of printing it * If only a single name/word is supplied it return that word as it is * Fixed errors reported by @Daque. I'm at least not able to recreate them anymore using the examples[/b] Generate possible usernames - RogueCoder - 05-16-2013 This script generates possible username combinations based on whatever you feed the script with, but probably most optimal for real names. That's at least what the intentional use was It can take a single name and a file with one name per line./username.py John Doe ./username.py -f names.txt (Not restricted to .txt) PS: It's my very first time writing code in Python so please feel free to suggest how to improve, optimize and clean the code ![]() Code: #!/usr/bin/python
import sys
def makeUsernames(names):
result = []
for name in names:
name = name.replace("\n", '')
name = name.lower()
parts = name.split(' ')
fname = parts[0]
if len(parts) > 1:
lname = parts[len(parts) - 1]
result.append(fname + lname)
result.append(lname + fname)
result.append(fname[0] + lname)
result.append(fname + lname[0])
result.append(lname[0] + fname)
result.append(lname + fname[0])
result.append(fname + '.' + lname)
result.append(lname + '.' + fname)
result.append(fname[0] + '.' + lname)
result.append(lname[0] + '.' + fname)
result.append(fname + '_' + lname)
result.append(lname + '_' + fname)
result.append(fname[0] + '_' + lname)
result.append(lname[0] + '_' + fname)
result.append(fname + '-' + lname)
result.append(lname + '-' + fname)
result.append(fname[0] + '-' + lname)
result.append(lname[0] + '-' + fname)
else:
result.append(fname)
return result
names = []
if len(sys.argv) < 2:
sys.exit('Syntax error: Not enough arguments')
elif sys.argv[1] == '-f':
if len(sys.argv) != 3:
sys.exit('Syntax error: You must supply an existing file')
else:
filename = sys.argv[2]
try:
with open(filename) as f:
names = f.readlines()
except IOError:
sys.exit('IOError: No such file or directory')
else:
names = [' '.join(sys.argv[1:])]
if len(names) > 0:
result = makeUsernames(names)
for name in result:
print name
else:
sys.exit('Unable to generate data')Some example outputs Spoiler: Single name "Jane Doe"Code: janedoe
doejane
jdoe
janed
djane
doej
jane.doe
doe.jane
j.doe
d.jane
jane_doe
doe_jane
j_doe
d_jane
jane-doe
doe-jane
j-doe
d-janeSpoiler: File with the names "John Doe" and "Eric Smith"Code: johndoe
doejohn
jdoe
johnd
djohn
doej
john.doe
doe.john
j.doe
d.john
john_doe
doe_john
j_doe
d_john
john-doe
doe-john
j-doe
d-john
ericsmith
smitheric
esmith
erics
seric
smithe
eric.smith
smith.eric
e.smith
s.eric
eric_smith
smith_eric
e_smith
s_eric
eric-smith
smith-eric
e-smith
s-ericUpdate: * Added sys.exit() on errors * The makeUsersnames() function now returns the value instead of printing it * If only a single name/word is supplied it return that word as it is * Fixed errors reported by @Daque. I'm at least not able to recreate them anymore using the examples[/b] RE: Generate possible usernames - Deque - 05-16-2013 That's a good idea. Simple and good. But the UI is not foolproof. This can not happen: Code: if sys.argv[2] == '':
print 'You must include a file'If you give not enough arguments, the array is just shorter, which results in an illegal access when you try to get the value at index 2, see here: Code: $ python gennames.py -f]
Traceback (most recent call last):
File "gennames.py", line 36, in <module>
if sys.argv[2] == '':
IndexError: list index out of rangeCheck for length of your array instead: Code: if len(sys.argv) != 3:Also while this works perfectly: Code: $ python gennames.py dataThis doesn't: Code: $ python gennames.py -f data
No such file or directory
Traceback (most recent call last):
File "gennames.py", line 49, in <module>
makeUsernames(names)
NameError: name 'names' is not definedThe file definitely exists. But even if it wouldn't exist, it is not good that there is a python error output for the user. This is because you call Code: makeUsernames(names)No matter what errors happened before. You should only call this function if the code before succeeded so far. For flexibility reasons and separation of concerns you should try to make your function makeUsernames(names) return a list of names instead of printing them. The UI and the logic should always be separated. RE: Generate possible usernames - Deque - 05-16-2013 That's a good idea. Simple and good. But the UI is not foolproof. This can not happen: Code: if sys.argv[2] == '':
print 'You must include a file'If you give not enough arguments, the array is just shorter, which results in an illegal access when you try to get the value at index 2, see here: Code: $ python gennames.py -f]
Traceback (most recent call last):
File "gennames.py", line 36, in <module>
if sys.argv[2] == '':
IndexError: list index out of rangeCheck for length of your array instead: Code: if len(sys.argv) != 3:Also while this works perfectly: Code: $ python gennames.py dataThis doesn't: Code: $ python gennames.py -f data
No such file or directory
Traceback (most recent call last):
File "gennames.py", line 49, in <module>
makeUsernames(names)
NameError: name 'names' is not definedThe file definitely exists. But even if it wouldn't exist, it is not good that there is a python error output for the user. This is because you call Code: makeUsernames(names)No matter what errors happened before. You should only call this function if the code before succeeded so far. For flexibility reasons and separation of concerns you should try to make your function makeUsernames(names) return a list of names instead of printing them. The UI and the logic should always be separated. RE: Generate possible usernames - RogueCoder - 05-16-2013 Thanks for the feedback @Deque! I'll try to fix up those things ![]() In the future I'll add support for middle names etc as well.. But for a complete rookie like myself, first and last name is more than enough to keep track of hehe
RE: Generate possible usernames - RogueCoder - 05-16-2013 Thanks for the feedback @Deque! I'll try to fix up those things ![]() In the future I'll add support for middle names etc as well.. But for a complete rookie like myself, first and last name is more than enough to keep track of hehe
RE: Generate possible usernames - MrGeek - 05-16-2013 Nice idea bro ![]() Well done ! Thanks for sis @Deque . I learned something . RE: Generate possible usernames - MrGeek - 05-16-2013 Nice idea bro ![]() Well done ! Thanks for sis @Deque . I learned something . RE: Generate possible usernames - RogueCoder - 05-16-2013 Updated the code in the OP If you have any ideas on missing combinations that should be in it, please let me know.. Again, thanks for your feedbacks ![]() Btw @Deque, I renamed my names.txt to just names and it did work as expected.. Can you please check again to see if it's still happening? RE: Generate possible usernames - RogueCoder - 05-16-2013 Updated the code in the OP If you have any ideas on missing combinations that should be in it, please let me know.. Again, thanks for your feedbacks ![]() Btw @Deque, I renamed my names.txt to just names and it did work as expected.. Can you please check again to see if it's still happening? |