Ignore str.format(**foo) if key doesn't exist in foo
Asked Answered
K

4

5

I am trying to get an email template together. The message content will be dependent on values within a dictionary. However, the dictionary might not contain all the keys each time.

This currently is fine as all values are in the dictionary ('Title', 'Surname', 'Additional Details'):

practise_dict = {"Additional Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}

msg = """From: John Smith <[email protected]>
To: {Title} {Surname} <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
{Additional Details}
<b>This is HTML message.</b>
<h1>This is headline.</h1>
`""".format(**practise_dict)

print(msg)

In the msg variable I am trying to create my 'template'. This means that I need to have all possible items that could be in the dictionary.

For example the next piece would fail as it is looking for 'Date' that doesn't exist in this dictionary:

practise_dict = {"Additional Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}

msg = """From: John Smith <[email protected]>
To: {Title} {Surname} <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
{Additional Details}
{Date}
<b>This is HTML message.</b>
<h1>This is headline.</h1>
`""".format(**practise_dict)

print(msg)

Is there a way to ask it to ignore a string substitution if it doesn't exist as a key in the look-up dictionary?

Kailey answered 22/1, 2015 at 17:5 Comment(1)
I
9

You can use Template and safe_substitute for that:

from string import Template

practise_dict = {"Additional_Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}

msg = """From: John Smith <[email protected]>
To: $Title $Surname <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
$Additional_Details
$Date
<b>This is HTML message.</b>
<h1>This is headline.</h1>
`"""

s = Template(msg).safe_substitute(**practise_dict)
print(s)

OUTPUT

From: John Smith <[email protected]>
To: Mr Smith <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
blah blah blah blah
$Date
<b>This is HTML message.</b>
<h1>This is headline.</h1>
Its answered 22/1, 2015 at 17:21 Comment(2)
This is great. Thank you. I've actually used a combination of the solutions. I'm using this idea of the defaults dictionary and safe_substitute. Thank you both.Kailey
An excellent replacement for envsubst(1). Much appreciated.Toscanini
I
2

I don't think there's a way to tell format that some substitutions are optional. However, here are two workarounds.

If there's only one missing field, try something like:

msg = """...""".format(Date=practise_dict.pop("Date", ""), # handle optional field
                       **practise_dict) # pass the rest as before

However, note that this modifies practise_dict, and will be awkward if there are several values to deal with.

If there may be multiple fields missing, or you don't want to modify the dictionary, the following would probably be preferable:

defaults = {"Date": "", "Additional Details": ""} # define optional fields
defaults.update(practise_dict) # override user data where available
msg = """...""".format(**defaults)

Both approaches will still fail for keys you haven't explicitly provided an alternative for, which allows you to make some fields mandatory.

Istria answered 22/1, 2015 at 17:8 Comment(0)
P
1

In case you don't know the keys yet:

You can just write a dict subclass, which will return the key if it was not found in the dictionary:

class FailsafeDict(dict):
    def __getitem__(self, item):
        try:
            return super().__getitem__(item)
        except KeyError:
            return "{" + str(item) + "}"
        # end try
    # end def
# end class

This works by overriding the __getitem__(item) function, which handles the dict[item] call.

You can than use it like this:

f = FailsafeDict({"yo": "lo", "foo": "bar"})
text = "{yo}! {lol}! {foo}!".format(**f)
print(text)  

Which prints lo! {lol}! bar!.

In your case that would be

msg = """...""".format(**FailsafeDict(practise_dict))
Phenetole answered 2/12, 2016 at 22:21 Comment(1)
text = "{yo}! {lol}! {foo}!".format(**f) still gives me KeyError. I am on Python 3.10.9.Cockhorse
A
0

I can't comment in @luckydonald answer so I will add it here my comment. format_map() should be used instead of classic format().

class FailsafeDict(dict):
    def __getitem__(self, item):
        try:
            return super().__getitem__(item)
        except KeyError:
            return "{" + str(item) + "}"

f = FailsafeDict({"yo": "lo", "foo": "bar"})
text = "{yo}! {lol}! {foo}!".format_map(f)
print(text) 

> "lo! {lol}! bar!"
Alduino answered 18/7, 2023 at 16:32 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Allegory

© 2022 - 2024 — McMap. All rights reserved.