Last active
August 29, 2015 13:57
-
-
Save joshbode/9543330 to your computer and use it in GitHub Desktop.
String template class with Oracle/SAS-style substitution rules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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