Easily Creating Strings with Templating

The CDAT software was developed by LLNL. This tutorial was written by Charles Doutriaux. This work was performed under the auspices of the U.S. Department of Energy by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.

Download the Jupyter Notebook

Table of Contents

Define Your String Template

Back to Top

In [1]:
import genutil
# Define your string template
file_template = "%(path)/%(variable).%(model).nc"
# Create a StringConstructor object
S = genutil.StringConstructor(file_template)
# Fill the StringConstructor with the values for each key
S.path = "blah"
S.variable = "tas"
S.model= "CNRM"
my_path = S()
print("My file path is:",my_path)
My file path is: blah/tas.CNRM.nc
In [2]:
# You can also pass keys at retrieval time
print("My file path for variable 'pr' is:",S(variable="pr"))
My file path for variable 'pr' is: blah/pr.CNRM.nc
In [3]:
# You can easily loop through keys like this:
for model in ["modelA","modelB","modelC"]:
    for variable in ["tas","pr","zg","u","v"]:
        S.model = model
        path = S(variable=variable)
        print("PATH IS:",path)
PATH IS: blah/tas.modelA.nc
PATH IS: blah/pr.modelA.nc
PATH IS: blah/zg.modelA.nc
PATH IS: blah/u.modelA.nc
PATH IS: blah/v.modelA.nc
PATH IS: blah/tas.modelB.nc
PATH IS: blah/pr.modelB.nc
PATH IS: blah/zg.modelB.nc
PATH IS: blah/u.modelB.nc
PATH IS: blah/v.modelB.nc
PATH IS: blah/tas.modelC.nc
PATH IS: blah/pr.modelC.nc
PATH IS: blah/zg.modelC.nc
PATH IS: blah/u.modelC.nc
PATH IS: blah/v.modelC.nc
In [4]:
# in some case you can even reverse ingeneer
print("key/values pairs:",S.reverse(path))
key/values pairs: {'path': 'blah', 'variable': 'v', 'model': 'modelC'}