Author Topic: [Python] Random name generator  (Read 4302 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[Python] Random name generator
« on: 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/

Code: [Select]
'''
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!'
#==============================


Offline puddi

  • Voted Best Avatar
  • VIP
  • Royal Highness
  • *
  • Posts: 662
  • Cookies: -2074
  • Stop being a fag today!Join #puddimasterrace @ IRC
    • View Profile
Re: [Python] Random name generator
« Reply #1 on: June 13, 2011, 12:46:52 pm »
thanks :D +1

Do you got a cool story you would like to share bro?

The following users thanked this post: puddi

Offline 10n1z3d

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
    • View Profile
Re: [Python] Random name generator
« Reply #2 on: June 13, 2011, 05:16:05 pm »
The following lines are pretty messy:

Code: [Select]
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:

Code: [Select]
while len(GeneratedNameArray) <= number:
    #do_something()

Third, you are not checking for duplicate names. Also, using the choice() method from the random module would be much better instead of generating pseudo-random integers.

Therefore, the above code could just become:
Code: [Select]
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).
« Last Edit: June 13, 2011, 10:14:58 pm by 10n1z3d »
Code: [Select]
python -c "print ''.join(chr(x) for x in [int(oct(39)) + 2, 24 * 2, 313 % 203, 0x31, (2 ** 7) - 6, int('051'), (3 << 6) - 92])"

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [Python] Random name generator
« Reply #3 on: June 13, 2011, 06:26:08 pm »
<...> Also, using the 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