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