Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import re
  4.  
  5. r = re.compile(r'((?:[^{]+|\{\{)*)\{([^}]*)\}')
  6.  
  7. def sub(tmpl, d):
  8.     def f(m):
  9.         return m.group(1) + d[m.group(2)]
  10.  
  11.     return r.sub(f, tmpl)
  12.  
  13. for tmpl, d, want in (
  14.         ('', {}, ''),
  15.         ('foo', {}, 'foo'),
  16.         ('{', {}, '{'),
  17.         ('}', {}, '}'),
  18.         ('{{', {}, '{{'),
  19.         ('}}', {}, '}}'),
  20.         ('{}', {'': '✓'}, '✓'),
  21.         ('a{b{c}d', {'b{c': '✓'}, 'a✓d'),
  22.         ('a{b}}c}d', {'b': '✓'}, 'a✓}c}d'),   # `}}' isn't escaping `}'.
  23.         ('a{{bc {d.e.f} }}{{{...} z', {'d.e.f': 'def', '...': 'etc'}, 'a{{bc def }}{{etc z'),
  24.     ):
  25.     got = sub(tmpl, d)
  26.     if got != want:
  27.         print(tmpl, d, want, got)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement