我想讓代碼更緊湊。我認爲有一部分可以很好地減少,但不知道如何去做。我在下面複製的代碼中添加註釋,以便快速找到它。如何合成這個python函數
的代碼是下一個:
def change_to_mop_gen(infile, namedir="",charge=0, gnorm=None, separate=True, hamiltonian="PM6", calctype="opt", multiplicity="singlet", eps=None, cycles=None):
#armado de la salida
if calctype=="thermo(298,298,0)":
calctype1="thermo"
else:
calctype1=calctype
outfile=infile.split(".")[0] + "-" + calctype1 + "-" + hamiltonian
#~ here comes the set of if/else sentences i think could be reduced
if gnorm !=None:
gnorm="gnorm=" + str(gnorm)
else:
gnorm= " "
if cycles!=None:
cycles="cycles="+str(cycles)
else:
cycles=""
if separate==True:
outfile=outfile + "_*.mop"
else:
outfile=outfile + ".mop"
if eps !=None:
eps="eps=" + str(eps)
else:
eps = ""
#~here it ends
keywords = "-xk '%s %s %s %s %s %s charge=%i'"%(cycles, hamiltonian, calctype, multiplicity, eps, gnorm, charge)
#change directory of outfile
if namedir!="":
outfile= namedir + "/" + outfile
return change_format(infile,outfile, keywords)
任何幫助將是一件好事。
我想你想在不在tex.stackexchange中的計算器。如果gnorm是None:''None'比較 – percusse
'gnorm =「」if gnorm is None else「gnorm =」+ str(gnorm)' – percusse
@percusse是的,我很抱歉 – HernanProust