def test_template_replace(): # default 'dct' mode templ_txt = "%(foo)i %(bar)s" rules = {'foo': 1, 'bar': 'lala'} tgt_txt = template_replace(templ_txt, rules, mode='dct') assert tgt_txt == "1 lala" # 'txt' mode, not default, but actually more often used b/c placeholders # are much simpler (no type formatting string, just convert values with # str() or pass in string-only values in `rules`). templ_txt = "XXXFOO XXXBAR" rules = {'XXXFOO': 1, 'XXXBAR': 'lala'} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=True) assert tgt_txt == "1 lala" # string-only is required, note that conv=False templ_txt = "XXXFOO" rules = {'XXXFOO': str(1)} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=False) assert tgt_txt == "1" # warn but pass not found placeholders in `rules` templ_txt = "XXXFOO XXXBAR" rules = {'XXXFOO': 1, 'XXXBAR': 'lala', 'XXXBAZ': 3} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=True) assert tgt_txt == "1 lala" # warn but do duplicate placeholders templ_txt = "XXXFOO XXXBAR XXXBAR" rules = {'XXXFOO': 1, 'XXXBAR': 'lala'} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=True) assert tgt_txt == "1 lala lala"
def test_dict(): # default 'dct' mode templ_txt = "%(foo)i %(bar)s" rules = {'foo': 1, 'bar': 'lala'} ref_txt = "1 lala" tgt_txt = template_replace(templ_txt, rules, mode='dct') assert tgt_txt == ref_txt, ("ref_txt={} " "tgt_txt={}".format(ref_txt, tgt_txt))
def test_mode_txt_conv_true_warn_more_placeholders(): # warn but do duplicate placeholders templ_txt = "XXXFOO XXXBAR XXXBAR" rules = {'XXXFOO': 1, 'XXXBAR': 'lala'} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=True) ref_txt = "1 lala lala" assert tgt_txt == ref_txt, ("ref_txt={} " "tgt_txt={}".format(ref_txt, tgt_txt))
def test_mode_txt_conv_true_warn_more_rules(): # warn but pass not found placeholders in `rules` templ_txt = "XXXFOO XXXBAR" rules = {'XXXFOO': 1, 'XXXBAR': 'lala', 'XXXBAZ': 3} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=True) ref_txt = "1 lala" assert tgt_txt == ref_txt, ("ref_txt={} " "tgt_txt={}".format(ref_txt, tgt_txt))
def test_mode_txt_conv_true_single(): # string-only is required, note that conv=False templ_txt = "XXXFOO" rules = {'XXXFOO': str(1)} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=False) ref_txt = "1" assert tgt_txt == ref_txt, ("ref_txt={} " "tgt_txt={}".format(ref_txt, tgt_txt))
def test_mode_txt_conv_true(): # 'txt' mode, not default, but actually more often used b/c placeholders # are much simpler (no type formatting string, just convert values with # str() or pass in string-only values in `rules`). templ_txt = "XXXFOO XXXBAR" rules = {'XXXFOO': 1, 'XXXBAR': 'lala'} tgt_txt = template_replace(templ_txt, rules, mode='txt', conv=True) ref_txt = "1 lala" assert tgt_txt == ref_txt, ("ref_txt={} " "tgt_txt={}".format(ref_txt, tgt_txt))
def write(self, dct, calc_dir='calc', mode='dct'): """Write file self.filename (e.g. calc/0/pw.in) by replacing placeholders in the template (e.g. calc.templ/pw.in). Parameters ---------- dct : dict key-value pairs, dct.keys() are converted to placeholders with self.func() calc_dir : str the dir where to write the target file to mode : str, {'dct', 'sql'} | mode='dct': replacement values are dct[<key>] | mode='sql': replacement values are dct[<key>].fileval and every | dct[<key>] is an SQLEntry instance """ assert mode in ['dct', 'sql'], ("Wrong 'mode' kwarg, use 'dct' " "or 'sql'") # copy_only : bypass reading the file and passing the text thru the # replacement machinery and getting the text back, unchanged. While # this works, it is slower and useless. if self.keys == []: _keys = None txt = None copy_only = True else: if self.keys is None: _keys = dct.iterkeys() warn_not_found = False else: _keys = self.keys warn_not_found = True if self.txt is None: txt = common.file_read(self.filename) else: txt = self.txt copy_only = False tgt = pj(calc_dir, self.basename) verbose("write: %s" %tgt) if copy_only: verbose("write: ignoring input, just copying file to %s" %(self.filename, tgt)) shutil.copy(self.filename, tgt) else: rules = {} for key in _keys: if mode == 'dct': rules[self.func(key)] = dct[key] elif mode == 'sql': # dct = sql_record, a list of SQLEntry's rules[self.func(key)] = dct[key].fileval else: raise StandardError("'mode' must be wrong") new_txt = common.template_replace(txt, rules, mode='txt', conv=True, warn_not_found=warn_not_found, warn_mult_found=False, disp=False) common.file_write(tgt, new_txt)
flfrq='XXXFNFREQ' / XXXNKS XXXKS """ matdyn_in_fn = 'matdyn.disp.in' matdyn_freq_fn = 'matdyn.freq.disp' mass_str = '\n'.join("amass(%i)=%e" %(ii+1,m) for ii,m in \ enumerate(st.mass_unique)) rules = {'XXXNKS': ks_path.shape[0], 'XXXKS': common.str_arr(ks_path), 'XXXMASS': mass_str, 'XXXFNFREQ': matdyn_freq_fn, } txt = common.template_replace(templ_txt, rules, conv=True, mode='txt') common.file_write(matdyn_in_fn, txt) common.system("gunzip q2r.fc.gz; matdyn.x < %s; gzip q2r.fc" %matdyn_in_fn) # parse matdyn output and plot # define special points path, used in plot_dis() to plot lines at special # points and make x-labels sp = kpath.SpecialPointsPath(ks=sp_points, ks_frac=sp_points_frac, symbols=sp_symbols) # QE 4.x, 5.x ks, freqs = pwscf.read_matdyn_freq(matdyn_freq_fn) fig,ax = kpath.plot_dis(kpath.get_path_norm(ks_path), freqs, sp, marker='', ls='-', color='k')
flfrq='XXXFNFREQ' / XXXNKS XXXKS """ matdyn_in_fn = 'matdyn.disp.in' matdyn_freq_fn = 'matdyn.freq.disp' mass_str = '\n'.join("amass(%i)=%e" %(ii+1,m) for ii,m in \ enumerate(st.mass_unique)) rules = { 'XXXNKS': ks_path.shape[0], 'XXXKS': common.str_arr(ks_path), 'XXXMASS': mass_str, 'XXXFNFREQ': matdyn_freq_fn, } txt = common.template_replace(templ_txt, rules, conv=True, mode='txt') common.file_write(matdyn_in_fn, txt) common.system("gunzip q2r.fc.gz; matdyn.x < %s; gzip q2r.fc" % matdyn_in_fn) # parse matdyn output and plot # define special points path, used in plot_dis() to plot lines at special # points and make x-labels sp = kpath.SpecialPointsPath(ks=sp_points, ks_frac=sp_points_frac, symbols=sp_symbols) # QE 4.x, 5.x ks, freqs = pwscf.read_matdyn_freq(matdyn_freq_fn) fig, ax = kpath.plot_dis(kpath.get_path_norm(ks_path), freqs,
def write(self, dct, calc_dir='calc', mode='dct'): """Write file self.filename (e.g. calc/0/pw.in) by replacing placeholders in the template (e.g. calc.templ/pw.in). Parameters ---------- dct : dict key-value pairs, dct.keys() are converted to placeholders with self.func() calc_dir : str the dir where to write the target file to mode : str, {'dct', 'sql'} | mode='dct': replacement values are dct[<key>] | mode='sql': replacement values are dct[<key>].fileval and every | dct[<key>] is an SQLEntry instance """ assert mode in ['dct', 'sql'], ("Wrong 'mode' kwarg, use 'dct' " "or 'sql'") # copy_only : bypass reading the file and passing the text thru the # replacement machinery and getting the text back, unchanged. While # this works, it is slower and useless. if self.keys == []: _keys = None txt = None copy_only = True else: if self.keys is None: _keys = dct.keys() warn_not_found = False else: _keys = self.keys warn_not_found = True if self.txt is None: txt = common.file_read(self.filename) else: txt = self.txt copy_only = False tgt = pj(calc_dir, self.basename) verbose("write: %s" % tgt) if copy_only: verbose("write: ignoring input, just copying file to %s" % (self.filename, tgt)) shutil.copy(self.filename, tgt) else: rules = {} for key in _keys: if mode == 'dct': rules[self.func(key)] = dct[key] elif mode == 'sql': # dct = sql_record, a list of SQLEntry's rules[self.func(key)] = dct[key].fileval else: raise Exception("'mode' must be wrong") new_txt = common.template_replace(txt, rules, mode='txt', conv=True, warn_not_found=warn_not_found, warn_mult_found=False, disp=False) common.file_write(tgt, new_txt)