Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.02 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Simple random name generator in Python
  2. # create the dict and save it to a file
  3. d={
  4. 'part1':[
  5.     'Ae',
  6.     'Di',
  7.     'Mo',
  8.     'Fam',],
  9. 'part2':[
  10.     'dar',
  11.     'kil',
  12.     'glar',
  13.     'tres',],
  14. }
  15.  
  16. import pickle
  17. f=open('syllables','w')
  18. pickle.dump(d,f)
  19. f.close()
  20.  
  21.  
  22. # read the dict back in from the file
  23. f1=open('syllables','r')
  24. sd=pickle.load(f1)
  25. f1.close()
  26.  
  27. import random
  28. first_part=sd['part1'][random.randint(0,len(sd['part1'])-1)]
  29. second_part=sd['part2'][random.randint(0,len(sd['part2'])-1)]
  30.  
  31. print '%s%s'%(first_part,second_part)
  32.        
  33. # create the dict and save it to a file
  34. d={
  35. 'part1':[
  36.     'Ae',
  37.     'Di',
  38.     'Mo',
  39.     'Fam',],
  40. 'part2':[
  41.     'dar',
  42.     'kil',
  43.     'glar',
  44.     'tres',],
  45. }
  46.  
  47. import pickle
  48. f=open('syllables','w')
  49. pickle.dump(d,f)
  50. f.close()
  51.  
  52.  
  53. # read the dict back in from the file
  54. f1=open('syllables','r')
  55. sd=pickle.load(f1)
  56. f1.close()
  57.  
  58. import random
  59. first_part=sd['part1'][random.randint(0,len(sd['part1'])-1)]
  60. second_part=sd['part2'][random.randint(0,len(sd['part2'])-1)]
  61.  
  62. print '%s%s'%(first_part,second_part)
  63.        
  64. import random
  65. parts = {}
  66.  
  67. with open('parts.txt', 'r') as f:
  68.     currentList = []
  69.     for line in f.readlines():
  70.         line = line.strip()
  71.         if line.startswith('[') and line.endswith(']'):
  72.             currentList = []
  73.             parts[line[1:-1]] = currentList
  74.         else:
  75.             currentList.append(line.strip())
  76.  
  77.  
  78. for i in xrange(10):    
  79.     print ''.join(random.choice(parts[partName]) for partName in sorted(parts))
  80.        
  81. Aekil
  82. Didar
  83. Mokil
  84. Mokil
  85. Moglar
  86. Moglar
  87. Diglar
  88. Famdar
  89. Famdar
  90. Modar
  91.        
  92. #!/usr/bin/env python
  93. import fileinput
  94. import random
  95. import re
  96. from collections import defaultdict
  97.  
  98. partname = ''
  99. parts = defaultdict(list)
  100. for line in fileinput.input():
  101.     line = line.rstrip()
  102.     if line.startswith('[') and re.match(r'[partd+]', line):
  103.         partname = line
  104.     else:
  105.         parts[partname].append(line)
  106.  
  107. parts_list = list(map(parts.get, sorted(parts)))
  108. for _ in range(10):
  109.     print(''.join(map(random.choice, parts_list)))
  110.        
  111. Famglar
  112. Famkil
  113. Didar
  114. Ditres
  115. Aedar
  116. Famglar
  117. Ditres
  118. Famtres
  119. Ditres
  120. Modar