SHARE
TWEET

Untitled

a guest Apr 21st, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top