Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save joshbode/9543330 to your computer and use it in GitHub Desktop.

Select an option

Save joshbode/9543330 to your computer and use it in GitHub Desktop.
String template class with Oracle/SAS-style substitution rules.
from string import Template
class OracleTemplate(string.Template):
"""Substitution variables, ala Oracle SQL*Plus"""
pattern = r"""
&(?:
(?P<escaped>&) |
(?P<braced>[_a-z0-9]+)\. |
(?P<named>[_a-z0-9]+) |
(?P<invalid>)
)
"""
delimiter = '&'
idpattern = '[_a-z0-9]+'
# example
t = OracleTemplate("""\
SELECT
t.*
FROM
&schema..transactions t
WHERE
t.state = '&state'
;
""")
print t.substitute(schema='prod', state='VIC')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment