0

I am trying to replace the following using the python Template module.

start_date BETWEEN DATEADD(days,-14,'${DATE_YYYY-MM-DD}') AND '${DATE_YYYY-MM-DD}'

I am using the ConfigParser to read the values and store it into a dictionary(config_params) which i am successfully able to do. Then I am doing a safe substitute to substitute into the above template but it doesnt seem to do anything. It replaces lots of other parameters in the file but just does not seem to replace ${DATE_YYYY-MM-DD}

The code that I am using is below:

with open(templateFile, 'r+') as f:
    temp = Template(f.read())
    resultFile = temp.safe_substitute(config_params)

Any help on why this is happening ? Is it the () that it does not like ?

TemplateFile:

SELECT
x
,y
,z
,a
,b
INTO ${od}.${tab}
FROM
mphd.${pd} as h
WHERE
a BETWEEN DATEADD(month,-12,'${DATE_YYYY-MM-DD}') AND '${DATE_YYYY-MM-    DD}'
;

Config File:

[GeneralParams]
od = sandbox
tab = abcd
pd = hierarchy_expanded
DATE_YYYY-MM-DD = 2016-08-05

config_params:

{'od': 'sandbox', 'DATE_YYYY-MM-DD': '2016-08-05', 'pd': 'hierarchy_expanded', 'tab': 'abcd'}

Result File:

SELECT
x
,y
,z
,a
,b
INTO sandbox.abcd
FROM
mphd.hierarchy_expanded as h
WHERE
a BETWEEN DATEADD(month,-12,'${DATE_YYYY-MM-DD}') AND '${DATE_YYYY-MM- DD}'
;
2
  • Please edit your post to add the content of templateFile, the value of config_params, and the value of resultFile after the script is run. Commented Aug 5, 2016 at 22:15
  • I have edited the question. Commented Aug 5, 2016 at 22:37

1 Answer 1

1

According to the doc, the default pattern that identifiers should follow is: [_a-z][_a-z0-9]*. Using substitute instead of safe_substitute warns about this.

However, you can fix that, by creating a new template class:

class MyTemplate(Template):
    idpattern = '[_a-z][_a-z0-9-]*' # Note the extra -

Then us MyTemplate (or whatever you want to name it) in your code instead of Template).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.