Generate possible usernames 05-16-2013, 11:41 AM
#1
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
Some example outputs
Update:
* 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]
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]




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)