Advertisement
rfmonk

string_template_newsyntax_BAD.py

Dec 29th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # this is all fucked up!
  3. import re
  4. import string
  5.  
  6. t = string.Template('$var')
  7. print t.patern.patern
  8.  
  9. #\$(?:
  10. #    (?P<escaped>\$) |
  11. #delimiters
  12. #    (?P<named>[_a-z][_a-z0-9]*)      |
  13. #identifier
  14. #    {(?P<braced>[_a-z][_a-z0-9]*)}   |
  15. #identifier
  16. #    (?P<invalid>)
  17. #ill-formed delimeter exprs
  18. #)
  19.  
  20.  
  21. class MyTemplate(string.Template):
  22.     delimeter = '{{'
  23.     pattern = r'''
  24.    \{\{(?:
  25.    (?P<escaped>\{\{)|
  26.    (?P<named>[_a-z][_a-z0-9]*)\}\}|
  27.    (?P<braced>[_a-z][_a-z0-9]*)\}\}|
  28.    (?P<invalid>)
  29.    )
  30.    '''
  31.  
  32. t = MyTemplate('''
  33. {{{{
  34. {{var}}
  35. ''')
  36.  
  37. print 'MATCHES:',
  38. t.pattern.findall(t.template)
  39. print 'SUBSTITUTED:',
  40. t.safe_substitute(var='replacement')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement