EvilZone
Programming and Scripting => Scripting Languages => : Kulverstukas June 13, 2011, 12:44:52 PM
-
puddi requested this script, so I made it.
This script takes a list of names and generates a new list by concatenating 2 names. Nothing fancy.
Comments on the code?
Project folder:
http://newage.ql.lt/projects/python/RandomName/ (http://newage.ql.lt/projects/python/RandomName/)
'''
Created on: 2011-06-13
Last update: nil
Kulverstukas
'''
import sys
import os
import string
import random
#==============================
def GetFromArg(What, FromWhere):
for GotArg in FromWhere:
if string.find(GotArg, What) != -1:
GotArg = string.replace(GotArg, What, '')
break
return GotArg
#==============================
print
print 'Random name generator - takes a list, randomly picks out 2 words and puts them together to make a full name'
print 'Command line arguments: --list=NAME -- file with names'
print ' --number=X -- number of names to generate'
print 'This script automagicaly writes everything to a file where the script was ran'
#==============================
arguments = sys.argv[1:]
if len(arguments) < 2:
print 'Not enough arguments, nigger!'
exit()
#==============================
list = GetFromArg('--list=',arguments)
number = GetFromArg('--number=',arguments)
number = int(number)
#==============================
if os.path.isdir(list) == True:
print 'File does not exist!'
exit()
try:
ListOfNames = [] # initiate the array
FileWithNames = file(list,'r') # assign the file
for Temp in FileWithNames: # read whole file into an array
ListOfNames.append(string.strip(Temp))
FileWithNames.close()
except:
print 'Something went wrong :( exiting...'
exit()
#==============================
TempInt = 0
GeneratedNameArray = [] # initiate another array
GeneratedName = ''
while TempInt != number:
RandNumb1 = random.randint(1,len(ListOfNames)-1)
RandNumb2 = random.randint(1,len(ListOfNames)-1)
GeneratedNameArray.append(ListOfNames[RandNumb1]+' '+ListOfNames[RandNumb2])
TempInt = TempInt + 1
#==============================
FileWithNames = file('Generated_names','w')
for Temp in GeneratedNameArray: FileWithNames.write(Temp+'\r\n')
FileWithNames.close()
print
print 'Successfully generated '+str(number)+' names!'
#==============================
-
thanks :D +1
-
The following lines are pretty messy:
TempInt = 0
GeneratedNameArray = [] # initiate another array
GeneratedName = ''
while TempInt != number:
RandNumb1 = random.randint(1,len(ListOfNames)-1)
RandNumb2 = random.randint(1,len(ListOfNames)-1)
GeneratedNameArray.append(ListOfNames[RandNumb1]+' '+ListOfNames[RandNumb2])
TempInt = TempInt + 1
First, you have declared the GeneratedName variable, but you are not using it anywere in the code. Second, your while loop doesn't look right, something iike this would be better:
while len(GeneratedNameArray) <= number:
#do_something()
Third, you are not checking for duplicate names. Also, using the choice() (http://docs.python.org/library/random.html#random.choice) method from the random module would be much better instead of generating pseudo-random integers.
Therefore, the above code could just become:
GeneratedNameArray = []
while len(GeneratedNameArray) <= number:
name = '{0} {1}'.format(random.choice(ListOfNames), random.choice(ListOfNames))
if not name in GeneratedNameArray:
GeneratedNameArray.append(name)
There are more optimization which could be done like appending the names to the output file straight from the while loop and like I said in one of the other topics, the GetFromArg function is pretty useless. Again, you should definitely read about the naming conventions in Python and it's syntax (PEP8).
-
<...> Also, using the choice() (http://docs.python.org/library/random.html#random.choice) method from the random module would be much better instead of generating pseudo-random integers.
Thanks. It was just what I was looking for at the beginning, but I guess I just missed that element.
Reading thet PEP8 paper now :P