0

I am trying to create a mail body template dynamically based on some conditions.

body = """ New product has created by {user}
           This can be viewed by clicking here {link}
           Logs is here {link2}
       """

I need to format this string based on some conditions, like:

if 'user' in params:
    body.format(user='username')
if 'link' in params:
    body.format(link='new link')
if 'link2' in params:
    body.format(link2='new link2')

I know I can do like below,

body.format(user='username',link='new link',link2='new link2')

But i have to do this based on the above conditions. Is there any way to achieve this in python.?

I have found this and this didn't help me.

Thanks in advance.

hygull
  • 8,464
  • 2
  • 43
  • 52
Sakeer
  • 1,885
  • 3
  • 24
  • 43

4 Answers4

0

Yeah, I said to use lambdas in my comment but defining a function would be better for expandability. Here's your code:

def format_template(*args):
    user = 'default'
    link = 'something'
    link2 = 'something2'
    if 'user' in args:
        user = 'username'
    elif 'link' in args:
        link = 'link'
    elif 'link2' in args:
        link2 = 'link2'
    body = f"""
    New product has created by {user}
    This can be viewed by clicking here {link}
    Logs is here {link2}
    """
    return body
GeeTransit
  • 1,458
  • 9
  • 22
0

What about something like this?

params.setdefault('user', 'unknown user')
params.setdefault('link', 'unknown link')
params.setdefault('link2', 'unknown log link')
body = """ New product has created by {user}
       This can be viewed by clicking here {link}
       Logs is here {link2}
       """.format(**params)

Of course you can change the defaults ('unknown user', etc.) to whatever value you deem appropriate. If you're not allowed to modify params, you can also use params2=dict(params) and then operate on params2 instead.

CanterChin
  • 19
  • 2
0

What's wrong with the question you linked? This seems like it would work:

body = """ New product has created by {user}
           This can be viewed by clicking here {link}
           Logs is here {link2}
       """.format(user='username' if 'user' in params else '???',
                  link='new link' if 'link' in params else '???',
                  link2='new link2' if 'link2' in params else '???')
iz_
  • 15,923
  • 3
  • 25
  • 40
0

You can try like this.

Here params is a dictionary so you can iterate over its items and do the replacement.

>>> def format(body, **params):
...     for key, value in params.items():
...         rep = "{" + key + "}"
...         if rep in body:
...             body = body.replace(rep, value)
...     return body
...
>>>
>>> body = """ New product has created by {user}
...            This can be viewed by clicking here {link}
...            Logs is here {link2}
...        """
>>>
>>> print(format(body, user='username'))
 New product has created by username
           This can be viewed by clicking here {link}
           Logs is here {link2}

>>> print(format(body, link='new link'))
 New product has created by {user}
           This can be viewed by clicking here new link
           Logs is here {link2}

>>> print(format(body, link2='new link2'))
 New product has created by {user}
           This can be viewed by clicking here {link}
           Logs is here new link2

>>>
>>> print(format(body, link2='new link2', link="new link"))
 New product has created by {user}
           This can be viewed by clicking here new link
           Logs is here new link2

>>> print(format(body, link2='new link2', link="new link", user="username"))
 New product has created by username
           This can be viewed by clicking here new link
           Logs is here new link2

>>>
hygull
  • 8,464
  • 2
  • 43
  • 52