##################################### # 2510's SOFTWARE ART SCRIPT: ##################################### # # (c) 2012 Kyle Reynolds Conway # http://twentyfivetens.wordpress.com # http://kylerconway.wordpress.com # ##################################### # LICENSE: ##################################### # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # ##################################### # SPECIAL THANKS: ##################################### # # I'm *very* new to writing code and would have been unable to get this far without the help of various online communities, guides, and several helpful and willing friends on the python IRC channel. Specifically: # * KirkMcDonald # * buhman # * http://docs.python.org/tutorial/ # * http://wiki.python.org/moin/BeginnersGuide/NonProgrammers # * http://www.sthurlow.com/python/ # * And numerous posts on innumerable forums and blogs across the web. # ##################################### # TROUBLESHOOTING or OTHER ISSUES ##################################### # # I really won't be of much help if something goes wrong with this script. # I make no guarantees that it will work for you as intended. # It might even break your computer (I hope not, but who knows). # Use at your own risk. # - But modify to your hearts content. # - Improve for others lovingly # ##################################### # WHAT THIS SCRIPT IS SUPPOSED TO DO: ##################################### # # It allows a user to take a script written for the LaTeX Sides class (sides.cls) -- http://tug.ctan.org/tex-archive/macros/latex/contrib/sides -- and (with a little effort to add in some clear variables), have the order and selection of the lines randomized, as well as have the character names and props swapped randomly within those lines to produce a new structure for the old play (in the sides class LaTeX format). # ##################################### # THE FILES YOU NEED: ##################################### # # 1) text.txt = A completed draft of your play in *.txt file using the Sides.cls formatting. # 2) props.txt = One prop per line to be input into the new draft wherever the following variable appears in text.txt: #prop# # 3) characternames.txt = One character name per line to be input into the new draft wherever the following variable appears in text.txt: #name# # ##################################### # THE CODE: ##################################### from random import randint #Allows you to generate random numbers. import linecache #Allows you to pull lines from a file. import re #Allows use of regular expression functions. #This should return the total number of lines in a given file char_lines = len(open("characternames.txt").readlines()) prop_lines = len(open("props.txt").readlines()) find_line = len(open("text.txt").readlines()) #Create a function to generate a random number between 1 and EOF; then pull said random line from file # CHARACTER NAMES from characternames.txt def char_name(n): randline = randint(1,char_lines) # Generate a random number between (x,y) rand_name = linecache.getline('characternames.txt', randline).strip() #Pull a line from a file -- in this case a random line. return rand_name # PROPS from props.txt def prop_name(n): randline = randint(1,prop_lines) rand_prop = linecache.getline('props.txt', randline).strip() return rand_prop #Open a file (and a second file); replace words in file w/ random line from above; write over second file def rand_insert(): f = open("text.txt") o = open("text2.txt","w") #Using "a" will append the file... using "w" will write anew. while 1: line_pull = f.readline() if not line_pull: break line_pull = re.sub("#name#", char_name, line_pull) #Replace Character Names line_pull = re.sub("#prop#", prop_name, line_pull) #Replace Prop Items o.write(line_pull) o.close() return #Randomize lines -- allow duplicates -- output to new file #count = 0 o2 = open("text3.txt", "w") for i in range(find_line): #while count < find_line: rand_insert() select_rand = randint(1,find_line) pull_rand = linecache.getline("text2.txt", select_rand) # count = count + 1 o2.write(pull_rand) linecache.clearcache() o2.close()