tom@442: #!/usr/bin/env python tom@442: # encoding: utf-8 tom@442: """ tom@442: randomize_snrs.py tom@442: tom@442: Created by Thomas Walters on 2010-11-06. tom@442: """ tom@442: tom@442: import sys tom@442: import getopt tom@442: import re tom@442: import random tom@442: tom@442: help_message = ''' tom@442: Replace a string in each of two files with a string randomly selected from a list. tom@442: ''' tom@442: tom@442: replacement_strings = ('snr_0dB', tom@442: 'snr_3dB', tom@442: 'snr_6dB', tom@442: 'snr_9dB', tom@442: 'snr_12dB', tom@442: 'snr_15dB', tom@442: 'snr_18dB', tom@442: 'snr_21dB', tom@442: 'snr_24dB', tom@442: 'snr_27dB', tom@442: 'snr_30dB', tom@442: 'snr_33dB', tom@442: 'snr_36dB', tom@442: 'snr_39dB', tom@442: 'snr_42dB', tom@442: 'snr_45dB', tom@442: 'clean') tom@442: tom@442: class Usage(Exception): tom@442: def __init__(self, msg): tom@442: self.msg = msg tom@442: tom@442: tom@442: def main(argv=None): tom@442: if argv is None: tom@442: argv = sys.argv tom@442: try: tom@442: try: tom@442: opts, args = getopt.getopt(argv[1:], "hs:f:m:o:p:", ["help", "string_to_replace=", "input_script_file=", "input_mlf=", "output_script_file=", "output_mlf="]) tom@442: except getopt.error, msg: tom@442: raise Usage(msg) tom@442: tom@442: # option processing tom@442: string_to_replace = "" tom@442: script_file = "" tom@442: ml_file = "" tom@442: output_script_file = "" tom@442: output_ml_file = "" tom@442: for option, value in opts: tom@442: if option in ("-h", "--help"): tom@442: raise Usage(help_message) tom@442: if option in ("-s", "--string_to_replace"): tom@442: string_to_replace = value tom@442: if option in ("-f", "--input_script_file"): tom@442: script_file = value tom@442: if option in ("-o", "--outut_script_file"): tom@442: output_script_file = value tom@442: if option in ("-p", "--output_mlf"): tom@442: output_ml_file = value tom@442: if option in ("-m", "--input_mlf"): tom@442: ml_file = value tom@442: tom@442: except Usage, err: tom@442: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) tom@442: print >> sys.stderr, "\t for help use --help" tom@442: return 2 tom@442: tom@442: script = open(script_file, 'r') tom@442: mlf = open(ml_file, 'r') tom@442: out_script = open(output_script_file, 'w') tom@442: out_mlf = open(output_ml_file, 'w') tom@442: out_mlf.write(mlf.readline()) tom@442: for l in script: tom@442: replacement = random.choice(replacement_strings) tom@442: out_script.write(re.sub(string_to_replace, replacement, l)) tom@442: out_mlf.write(re.sub(string_to_replace, replacement, mlf.readline())) tom@442: out_mlf.write(mlf.readline()) tom@442: out_mlf.write(mlf.readline()) tom@442: out_mlf.write(mlf.readline()) tom@442: out_mlf.write(mlf.readline()) tom@442: tom@442: tom@442: tom@442: if __name__ == "__main__": tom@442: sys.exit(main())