Python Forum

Full Version: Rendering a list in a mako template
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
import os
from mako.template import Template

def N_water(z) : # return number of molecules of water [molecule/cm3] in tvs at position z [cm]
Na = 0.6022140857 #a vogadro's number [molecule/mole] * 1E-24
Mw = 18
return (Na * (3E-6 * z ** 3 + 1E-5 * z ** 2 - 0.2833 * z + 709.36) / (Mw * 1000))
def N_Hw(z) : # return number of H [atom/cm3] in water in tvs assembly at position z [cm]
return (2 * N_water(z))
def N_Ow(z): # return number of O [atom/cm3] in water in tvs assembly at position z [cm]
return (N_water(z))

N_HW=
for z in range(-185,186):
N_HW.append(round(N_Hw(z),6))
N_OW=
for z in range(-185,186):
N_OW.append(round(N_Ow(z),6))

for h in range(0, 370):
inp_dir = os.path.join(getera_dir, r"input\VVER_{0}.inp".format(h))
out_dir = os.path.join(getera_dir, r"input\VVER_{0}.out".format(h))
with open(cfg_dir, "w") as f:
f.write(cfg_templ.render(inp_dir=inp_dir, out_dir=out_dir))
with open(inp_dir, "w") as f:
f.write(inp_templ.render(N_HW[h], N_OW[h]))
os.system(r"C:\Users\mike\Desktop\VVER\GETERA\bin\getera.exe")
Error:
C:\Python36\python.exe C:/Users/mike/Desktop/VVER/tvs82/densities82.py Traceback (most recent call last): File "C:/Users/mike/Desktop/VVER/tvs82/densities82.py", line 177, in <module> f.write(inp_templ.render(N_HW[h], N_OW[h])) File "C:\Python36\lib\site-packages\mako\template.py", line 462, in render return runtime._render(self, self.callable_, args, data) File "C:\Python36\lib\site-packages\mako\runtime.py", line 838, in _render **_kwargs_for_callable(callable_, data)) File "C:\Python36\lib\site-packages\mako\runtime.py", line 873, in _render_context _exec_template(inherit, lclcontext, args=args, kwargs=kwargs) File "C:\Python36\lib\site-packages\mako\runtime.py", line 899, in _exec_template callable_(context, *args, **kwargs) TypeError: render_body() takes 1 positional argument but 3 were given Process finished with exit code 1
# there are another 150 lines of code but they irreverent, the problem is I cant render() my lists to the template
did you try to pass the arguments on line 26 as keyword arguments? It works on line 24
(Oct-25-2017, 04:16 PM)buran Wrote: [ -> ]did you try to pass the arguments on line 26 as keyword arguments? It works on line 24

yeah, I've tried like this, I got another error:
f.write(inp_templ.render(NHW=N_HW[h],NOW=N_OW[h]))
Error:
TypeError: 'Undefined' object does not support indexing

If anybody has another method of creating a mako template with lists from python, please tell me, i wasted my whole day just on this part....it seems so simple and basic but somehow i am missing it
print N_HW[h] and NOW=N_OW[h] to see what their values are... obviously they are not what you expect
and next time post the full traceback in error tags.
You may want to repost the code in code tags and preserved indentation too.
import os
from mako.template import Template

#====================================    DENSITIES   =======================================
# ===================================     COOLANT    =======================================
def N_water(z) :          # return number of molecules of water [molecule/cm3] in tvs assembly at position z [cm]
    Na =  0.6022140857    #a vogadro's number [molecule/mole] * 1E-24
    Mw = 18
    return (Na * (3E-6 * z ** 3 + 1E-5 * z ** 2 - 0.2833 * z + 709.36) / (Mw * 1000))
def N_Hw(z) :                    # return number of H [atom/cm3] in water in tvs assembly at position z [cm]
    return (2 * N_water(z))
def N_Ow(z):                    # return number of O [atom/cm3] in water in tvs assembly at position z [cm]
    return (N_water(z))

N_HW=[]
for z in range(-185,186):
    N_HW.append(round(N_Hw(z),6))
N_OW=[]
for z in range(-185,186):
    N_OW.append(round(N_Ow(z),6))
# ===================================    CLAD OF FUEL, ALLOY E110   ===========================
# ================================ also Central channel and Regulation channel alloy E635=======
def N_Zrf(z) :   # return number of molecules of Zr clad [molecule/cm3] in tvs assembly at position z [cm]
    Na =  0.6022140857
    Mzr= 91.224
    return (Na * (2E-07 * z**3 + 0.0002 * z**2 - 0.0176 * z + 6504.1) / (Mzr * 1000))
def N_Nbf(z):
    return (0.01 * N_Zrf(z))
N_ZRF=[]
for z in range(-185,186):
    N_ZRF.append(round(N_Zrf(z),6))
N_NBF=[]
for z in range(-185,186):
    N_NBF.append(round(N_Nbf(z),6))
#================================ Gap of Helium =================================================
def N_He(z):
    Na= 0.6022140857
    MHe= 4
    return(Na * (0.0009 * z**2 - 0.0718 * z + 5.8319) / (MHe *1000) )
N_HE=[]
for z in range(-185,186):
    N_HE.append(round(N_He(z),6))
# ==================================      FUEL              =======================================
# ================================== 300 TVEL 3.554 % ENRICHED UO2 ================================
def N_UO2(z):   # return number of molecules of UO2 in fuel [molecule/cm3] at position z [cm]
    Na =  0.6022140857
    Muo2= 270.03
    return (Na * (-2E-08 * z**4 + 4E-07 * z**3 + 0.0023 * z**2 - 0.0182 * z+ 10030) / (Muo2 * 1000))
def N_U235(z):  # return number of U235 atoms [atom/cm3] in fuel element at position z [cm]
    Mu235 = 235
    Mu = 270.03
    row = 0.03554
    return (row * N_UO2(z) * Mu / Mu235)
def N_U238(z):  # return number of u238 atoms [atom/cm3] in fuel element at position z [cm]
    return (N_UO2(z) - N_U235(z))
def N_Ofl(z):   # return number of O atoms [atom/cm3] in fuel element at position z [cm]
    return (2* N_UO2(z))


N_U235L=[]
for z in range(-185,186):
    N_U235L.append(round(N_U235(z),6))
N_U238L=[]
for z in range(-185,186):
    N_U238L.append(round(N_U238(z),6))
N_OFL=[]
for z in range(-185,186):
    N_OFL.append(round(N_Ofl(z),6))
# ================================== 12 TVEG 2.4% ENRICHED UO2 MIXED WITH 8% Gd2O3  ===============
def N_UO2g(z):   # return number of molecules of UO2 in tveg [molecule/cm3] at position z [cm]
    Na =  0.6022140857
    Muo2= 270.03
    x_uo2=0.935 # mole fraction of UO2
    return (x_uo2 * Na * (-2E-08 * z**4 + 3E-07 * z**3 + 0.0023 * z**2 - 0.018 * z + 9935.5) / (Muo2 * 1000))
def N_Gd2O3(z) :    # return number of molecules of gd2o3 in tveg [molecule/cm3] at position z [cm]
    Na =  0.6022140857
    Mgd2o3= 362.5
    x_uo2=0.935 # mole fraction of UO2
    x_gd2o3=1-x_uo2 # mole fraction of Gd2O3
    return (x_gd2o3 * Na * (-2E-08 * z ** 4 + 3E-07 * z ** 3 + 0.0023 * z ** 2 - 0.018 * z + 9935.5) / (Mgd2o3 * 1000))
def N_U235g(z):  # return number of U235 atoms [atom/cm3] in tveg at position z [cm]
    Mu235 = 235
    Mu = 238.02891
    row = 0.024
    return (row * N_UO2g(z) * Mu / Mu235)
def N_U238g(z):  # return number of u238 atoms [atom/cm3] in tveg at position z [cm]
    return (N_UO2g(z) - N_U235(z))
def N_Gd(z) :   # return number of Gd atoms [atom/cm3] in tveg at position z [cm]
    return ( 2 * N_Gd2O3(z))
def N_Gd57(z) :
    x_Gd=0.1565
    return (x_Gd * N_Gd(z))
def N_Ofg(z):   # return number of O atoms [atom/cm3] in tveg at position z [cm]
    return (( 2 * N_UO2g(z) ) + ( 3 * N_Gd2O3(z)))

N_U235G=[]
for z in range(-185,186):
    N_U235G.append(round(N_U235g(z),6))
N_U238G=[]
for z in range(-185,186):
    N_U238G.append(round(N_U238g(z),6))
N_GD57=[]
for z in range(-185,186):
    N_GD57.append(round(N_Gd57(z),6))
N_OFG=[]
for z in range(-185,186):
    N_OFG.append(round(N_Ofg(z),6))

# ==============================  control rod absorption material B4C + Dy2TiO5  ==================
Na =  0.6022140857
Mb4c= 55.255
row_b4c = 1700
N_B4C= Na * row_b4c * 0.001 /Mb4c
x_b10=0.199
N_B10= x_b10 * 4 * N_B4C
N_C= N_B4C
Mdy2tio5 = 452.9
N_Dy2TiO5=Na * 4900 * 0.001/ Mdy2tio5  # return number of molecules of Dy2TiO5 in Control rod [molecule/cm3]
N_Dy= 2 * N_Dy2TiO5
N_Ti= N_Dy2TiO5
N_Oc= 5 * N_Dy2TiO5

N_B10C=[round(0.0000001,6)]*140
N_B10C.extend([round(N_B10,6)]*45)  # 20% of active core height - 30 cm of Dy2TiO5

N_HC=[]
for z in range(-185,112):
    N_HC.append(round(N_Hw(z),6))
N_HC.extend([0.0]*74)  # when conrol rod is 20% of active core length, the rest of the channel is filled
                                      # with water
N_OCW=[]
for z in range(-185,112):
    N_OCW.append(round(N_Ow(z),6))
N_OCW.extend([0.0]*74)

N_CC=[round(0.0000001,6)]*140
N_CC.extend([round(N_C,6)]*45)

N_DY=[round(0.0000001,6)]*110
N_DY.extend([round(N_Dy,6)]*30)
N_DY.extend([round(0.0000001,6)]*45)

N_TI=[round(0.0000001,6)]*110
N_TI.extend([round(N_Ti,6)]*30)
N_TI.extend([round(0.0000001,6)]*45)

N_OC=[round(0.0000001,6)]*110
N_OC.extend([round(N_Oc,6)]*30)
N_OC.extend([round(0.0000001,6)]*45)
# ===============================       CONTROL ROD CLADDING, ALLOY 42XHM    ===========================

Na =  0.6022140857
Mni = 58.7
Mcr = 52
row_42xnm = 8
x_ni = 0.55
x_cr = 0.42
N_Ni= x_ni * Na * row_42xnm * 2 / (Mni + Mcr)
N_Cr= x_cr * Na * row_42xnm * 2 / (Mni + Mcr)
N_NI=[round(N_Ni,6)]*370
N_CR=[round(N_Cr,6)]*370

input_template_dir = r"C:\Users\mike\Desktop\VVER\tvs82\inputgetera82.inp"
getera_dir = r"C:\Users\mike\Desktop\VVER\GETERA"
cfg_dir = os.path.join(getera_dir, r"bin\config.get")
cfg_templ_dir = os.path.join(getera_dir, r"cfg.mako")

inp_templ = Template(filename = input_template_dir)
cfg_templ = Template(filename = cfg_templ_dir)

for h in range(0, 370):
    inp_dir = os.path.join(getera_dir, r"input\VVER_{0}.inp".format(h))
    out_dir = os.path.join(getera_dir, r"input\VVER_{0}.out".format(h))
    with open(cfg_dir, "w") as f:
        f.write(cfg_templ.render(inp_dir=inp_dir, out_dir=out_dir))
    with open(inp_dir, "w") as f:
        f.write(inp_templ.render(NHW[h],N_HW[h]))
    os.system(r"C:\Users\mike\Desktop\VVER\GETERA\bin\getera.exe")
Error:
C:\Python36\python.exe C:/Users/mike/Desktop/VVER/tvs82/densities82.py Traceback (most recent call last): File "C:/Users/mike/Desktop/VVER/tvs82/densities82.py", line 179, in <module> f.write(inp_templ.render(NHW[h],N_HW[h])) IndexError: list index out of range Process finished with exit code 1
Quote:there are 20 lists i need to render into the template
Quote:i am new in python, i am just using it to create input file for another code
in your code in post #5 you have
 
 f.write(inp_templ.render(NHW[h],N_HW[h])
however there is no NHW list in your code - this is the only place in the code where it appears. in Post #1 you have different one
f.write(inp_templ.render(N_HW[h], N_OW[h]))
So you should get NameError, because NHW i not defined.

also your list created with z in range (-185,186) have 371 elements, while h in range (0,370) will have only 370 elements so you will miss one element
(Oct-25-2017, 07:36 PM)buran Wrote: [ -> ]in your code in post #5 you have
 
 f.write(inp_templ.render(NHW[h],N_HW[h])
however there is no NHW list in your code - this is the only place in the code where it appears. in Post #1 you have different one
f.write(inp_templ.render(N_HW[h], N_OW[h]))
So you should get NameError, because NHW i not defined.

also your list created with z in range (-185,186) have 371 elements, while h in range (0,370) will have only 370 elements so you will miss one element

Quote:even after defining a list
NWH=[]
and changing range to (0,371) it still gives error
Error:
IndexError: list index out of range
, i am trying to change the syntax of render(), that's why post #1 is different from post #5
if you just define NWH=[] and don't populate it, it will give Index Error because there are no elements in the list
going back to first post
you have
f.write(cfg_templ.render(inp_dir=inp_dir, out_dir=out_dir))
which works
and
f.write(inp_templ.render(N_HW[h], N_OW[h]))
which does not work and raise the error in the first post.
I think you need to supply N_HW[h] and N_OW[h] as keyword arguments, like you do in the other line where you have inp_dir=inp_dir
(Oct-25-2017, 08:02 PM)buran Wrote: [ -> ]going back to first post
you have
f.write(cfg_templ.render(inp_dir=inp_dir, out_dir=out_dir))
which works
and
f.write(inp_templ.render(N_HW[h], N_OW[h]))
which does not work and raise the error in the first post.
I think you need to supply N_HW[h] and N_OW[h] as keyword arguments, like you do in the other line where you have inp_dir=inp_dir

f.write(inp_templ.render(NHW[h]=N_HW[h]))
Quote:Gives this error:
Error:
f.write(inp_templ.render(NHW[h]=N_HW[h])) ^ SyntaxError: keyword can't be an expression

(Oct-25-2017, 08:29 PM)getre222 Wrote: [ -> ]
(Oct-25-2017, 08:02 PM)buran Wrote: [ -> ]going back to first post
you have
f.write(cfg_templ.render(inp_dir=inp_dir, out_dir=out_dir))
which works
and
f.write(inp_templ.render(N_HW[h], N_OW[h]))
which does not work and raise the error in the first post.
I think you need to supply N_HW[h] and N_OW[h] as keyword arguments, like you do in the other line where you have inp_dir=inp_dir

f.write(inp_templ.render(NHW[h]=N_HW[h]))
Quote:Gives this error:
Error:
f.write(inp_templ.render(NHW[h]=N_HW[h])) ^ SyntaxError: keyword can't be an expression

f.write(inp_templ.render(NHW=N_HW[h]))
Quote:gives this error:
Error:
TypeError: 'Undefined' object does not support indexing
Pages: 1 2