In [10]:
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
In [11]:
# You can also pass keys at retrieval time
print "My file path for variable 'pr' is:",S(variable="pr")
In [15]:
# 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
In [17]:
# in some case you can even reverse ingeneer
print "key/values pairs:",S.reverse(path)
In [ ]: