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))

    >>> o = Duck(one='one', two='two', three=5)
    >>> format_obj(o, '%(one)s %(two)s %(three)d')
    'one two 5'
    
recipes/Python/576883_Dictlike_string_formatting_object_named/recipe-576883.py