Formats a string using the same notation as a dict-expansion, but using an object as the source for the expansion instead. Attributes from the object are used as source to the expansions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import re
exp_rx = re.compile('%\((\w+)\)')
def format_obj(obj, text):
"""
>>> o = Duck(one='one', two='two', three=5)
>>> format_obj(o, '%(one)s %(two)s %(three)d')
'one two 5'
"""
return text % dict((a, getattr(obj, a))
for a in re.findall(exp_rx, text))
# ---- Support for the doctest from here down:
class Duck(object):
"""Quack!"""
def __init__(self, **kw):
self.__dict__.update(kw)
if __name__ == '__main__':
import doctest
doctest.testmod()
|
I like over-riding the __repr__ method myself:
You can get fancier with it, e.g. skipping private attributes, but I mostly use it for development/debugging and remove it later.
Hello!
You got the same result with:
What is the advantage of your recipe?
Or even simpler: