0

I have a python dictionary as and I want to substitute the values in it.

dict = {
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":{circle}: <{log_url}| {task}>"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Dag Name:* {dag}"
                },
                {
                    "type": "mrkdwn",
                    "text": "*Execution Time:* {exec_time} UTC"
                }
            ]
        }
    ]
}

I want to substitute the values enclosed in {} such as circle, log_url and so on.

I have tried the following approach by converting it in string representation but I am getting exceptions.


dict = """
{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":{circle}: <{log_url}| {task}>"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Dag Name:* {dag}"
                },
                {
                    "type": "mrkdwn",
                    "text": "*Execution Time:* {exec_time} UTC"
                }
            ]
        }
    ]
}""".format(task="a",
        dag="b",
        exec_time="10",
        log_url="url",
        circle=circle)

But it it giving me errors. ERROR - '\n\t"blocks"' ... KeyError: '\n\t"blocks"'

How can I resolve it?

Complete code:

import json
import requests


def send_slack_success_metadata_alert():
    circle = 'green-tick'
    template = """{
        "blocks": [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": ":{circle}: <{log_url}| {task}>"
                }
            },
            {
                "type": "divider"
            },
            {
                "type": "section",
                "fields": [
                    {
                        "type": "mrkdwn",
                        "text": "*Dag Name:* {dag}"
                    },
                    {
                        "type": "mrkdwn",
                        "text": "*Execution Time:* {exec_time} UTC"
                    }
                ]
            }
        ]
    }""".format(
        task="T1",
        dag="ID",
        exec_time="10",
        log_url="dummyurl",
        circle=circle
    )
    print(template)
    send_alert("http://localhost:80",template)

def send_alert(url, message):
    requests.post(url=url, data=json.dumps({'text': message}),
                  headers={'Content-type': 'application/json'})

if __name__ == "__main__":
    print(send_slack_success_metadata_alert())
Avenger
  • 793
  • 11
  • 31
  • Can you post the entirety of the traceback error? – blackbrandt Jul 19 '21 at 13:48
  • In the first code your dict is an actual `dict` type. In the second one it is a string representation of a dict. Which one is it? – Tomerikoo Jul 19 '21 at 13:50
  • It is the first one, I was trying to substitute using the second one but it did not work – Avenger Jul 19 '21 at 13:53
  • The code is complete, running it will give complete error – eroot163pi Jul 19 '21 at 13:56
  • Also regardless of dict variable you will get same error and is surprising. I am not sure why -1 was given for this – eroot163pi Jul 19 '21 at 13:56
  • use your first actual dict with `"text": f":{circle}: <{log_url}| {task}>"`. Remember to clone the dict if you want a new one. – JonSG Jul 19 '21 at 13:58
  • Does this answer your question? [How can I print literal curly-brace characters in a string and also use .format on it?](https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-a-string-and-also-use-format) – Tomerikoo Jul 19 '21 at 14:02
  • @root163 It's not surprising at all. I didn't downvote but having un-escaped `{}` in your format string means python is going to try and replace it. – Axe319 Jul 19 '21 at 14:02
  • 1
    I see, I also noticed it now (the un-escaped {}). thanks @Axe319. Felt like honest mistake someone would make because I also could not figure out one first view – eroot163pi Jul 19 '21 at 14:04
  • @Tomerikoo I wasn't responding to the downvote. I just thought the error made it clear what the problem was but I may be mistaken. – Axe319 Jul 19 '21 at 14:05
  • 1
    @Axe319 Ah sorry. I might have misunderstood you. Anyway, you are right, see my proposed dupe above – Tomerikoo Jul 19 '21 at 14:07

1 Answers1

0

do you need to set the values at a later step? Or could f-strings be the solution for you?

task="a"
dag="b"
exec_time="10"
log_url="url"
circle=circle

dict = {
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": f":{circle}: <{log_url}| {task}>"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": f"*Dag Name:* {dag}"
                },
                {
                    "type": "mrkdwn",
                    "text": f"*Execution Time:* {exec_time} UTC"
                }
            ]
        }
    ]
}
schilli
  • 1,700
  • 1
  • 9
  • 17