alexwilsonphoto

rtable.py

Mar 2nd, 2015
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. #!/usr/bin/python
  2. import random, sys
  3.  
  4. if len(sys.argv)<2 or len(sys.argv)>3:
  5.     print 'usage: python ' + sys.argv[0]+ ' <filename> [count]'
  6.     sys.exit(-1)
  7.  
  8. class RRow:
  9.     def __init__(self):
  10.         self.weight = 0
  11.         self.val = ""
  12.  
  13. class RTable:
  14.     def __init__(self):
  15.         self.rows = []
  16.  
  17. class RTables:
  18.     def __init__(self):
  19.         self.tabledic = {}
  20.  
  21. seed = random.SystemRandom().randint(1,sys.maxint)
  22. random.seed(seed)
  23.  
  24. tables = RTables()
  25. with open(sys.argv[1], 'r') as f:
  26.     origoutput = f.readline().rstrip()
  27.     for line in f:
  28.         liner = line.rstrip()
  29.         if liner == "":
  30.             curtable = ""
  31.         elif curtable == "":
  32.             curtable = liner;
  33.             tables.tabledic[curtable] = RTable()
  34.         else:
  35.             curline = liner;
  36.             row = RRow()
  37.             row.weight = int(liner[0:liner.find(' ')])
  38.             row.val = liner[liner.find(' ')+1:len(liner)]
  39.             tables.tabledic[curtable].rows.append( row )
  40. f.closed
  41.  
  42. def getrrow( tablename ):
  43.     t = tables.tabledic[tablename]
  44.     totalweight = 0
  45.     for i in t.rows:
  46.         totalweight += i.weight
  47.     rnum = random.SystemRandom().randint(1,totalweight)
  48.     totalweight = 0
  49.     for i in t.rows:
  50.         totalweight += i.weight
  51.         if rnum<=totalweight:
  52.             return i.val;
  53.  
  54. if len(sys.argv)==3:
  55.     maxgen=int(sys.argv[2])
  56. else:
  57.     maxgen=1
  58.  
  59. for x in range ( 0, maxgen):
  60.     vars = {}
  61.     output=origoutput
  62.     while output.find('{')>-1:
  63.         bracket1 = output.find('{')
  64.         bracket2 = output.find('}')
  65.         inbrackets = output[bracket1+1:bracket2]
  66.         if inbrackets[0:1] == '%':
  67.             if inbrackets.find('=')>-1:
  68.                 varname = inbrackets[1:inbrackets.find('=')]
  69.                 vars[varname]=int(inbrackets[inbrackets.find('=')+1:len(inbrackets)])
  70.                 output = output[0:bracket1] + output[bracket2+1:len(output)]
  71.             elif inbrackets.find('+')>-1:
  72.                 varname = inbrackets[1:inbrackets.find('+')]
  73.                 vars[varname]+=int(inbrackets[inbrackets.find('+')+1:len(inbrackets)])
  74.                 output = output[0:bracket1] + output[bracket2+1:len(output)]
  75.             else:
  76.                 output = output[0:bracket1] + str(vars[inbrackets[1:len(inbrackets)]]) + output[bracket2+1:len(output)]
  77.         elif inbrackets[0:2] == '\\n':
  78.             output = output[0:bracket1] + '\n' + output[bracket2+1:len(output)]
  79.         else:
  80.             output = output[0:bracket1] + getrrow(inbrackets) + output[bracket2+1:len(output)]
  81.     print output
Advertisement
Add Comment
Please, Sign In to add comment