0

I am trying to replace the input with a variable based on user input (function_name). I tried replace but list does not allow that. Any other way to go about replacing this ?

function_name = sys.argv[1]
payload = [
    {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': 'avg:aws.lambda.duration.p50{resource:input}'}
            ],
            'title': 'p50',
            'title_size': '16'
        }
    },
        {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': 'avg:aws.lambda.duration.p80{resource:input}'}
            ],
        'title': 'p80'
        }
    }
]

5 Answers 5

2

If you are sure that you don't have string 'input' in other places in payload.

This will be easy way to replace.

import json
function_name = sys.argv[1]
payload = [
    {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': 'avg:aws.lambda.duration.p50{resource:input}'}
            ],
            'title': 'p50',
            'title_size': '16'
        }
    },
        {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': 'avg:aws.lambda.duration.p80{resource:input}'}
            ],
        'title': 'p80'
        }
    }
]
payload = json.loads(json.dumps(payload).replace('input', function_name))

don't forget to import json.

Otherwise, you will need to follow other's solution

Sign up to request clarification or add additional context in comments.

3 Comments

Heyy thanks! I am able to replace the input with ur given code, however there's this "TypeError: Cant concat dict to bytes". I assume its due to my code below: conn.request("POST", "/api/v1/dashboard", payload, headers) . Any idea whats the issue with this?
@Mervin Please give me code what you tried and let me know the python version you used. yeah, I think issue could ber raised from some other part in your code.
@Mervin You can upvote too if answer is helpful.
0

I'm not sure why you say .replace won't work. Just do it on the individual strings:

>>> function_name = "NAME"
>>> text = 'avg:aws.lambda.duration.p50{resource:input}'
>>> text.replace("input",function_name)
'avg:aws.lambda.duration.p50{resource:NAME}'

You can also just do the replace inline without assigning a variable:

>>> 'avg:aws.lambda.duration.p50{resource:input}'.replace("input",function_name)
'avg:aws.lambda.duration.p50{resource:NAME}'

If you were trying to do it to the list, then yes, you can't do that.

You might also want to look into f-strings.

>>> text = f'avg:aws.lambda.duration.p50{{resource:{function_name}}}'
>>> text
'avg:aws.lambda.duration.p50{resource:NAME}'

Comments

0

An extremely direct way of doing so would be to access that value of the dictionary in the list of the key of the dictionary of the list... In other words, isolate the string and use .replace()

for i in payload:
    i['definition']['requests'][0]['q'] = i['definition']['requests'][0]['q'].replace("input", function_name)

I'm sure there is a more general way, but this works.

Or if possible, rework your payload using string formatting:

payload = [
    {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': 'avg:aws.lambda.duration.p50{resource:%s}' % function_name}
            ],
            'title': 'p50',
            'title_size': '16'
        }
    },
        {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': 'avg:aws.lambda.duration.p80{resource:%s}' % function_name}
            ],
        'title': 'p80'
        }
    }
]

Comments

0

There are many ways to achieve this, one of which could be to use the %-formatting:

function_name = sys.argv[1]
payload = [
    {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': "avg:aws.lambda.duration.p50{resource:%s}" % function_name}
            ],
            'title': 'p50',
            'title_size': '16'
        }
    },
        {
        'definition': {
            'type': 'query_value',
            'requests': [
                {'q': "avg:aws.lambda.duration.p80{resource:%s}" % function_name}
            ],
        'title': 'p80'
        }
    }
]

Comments

0

You've got a lot of ways to do that. I give you three.

  1. F-string (the best for me):
q_input = input()
payload = f'avg:aws.lambda.duration.p50 {q_input}'
  1. String formatting (old style)
q_input = input()
payload = f'avg:aws.lambda.duration.p50 %s' % q_input
  1. String formatting (new style)
q_input = input()
payload = f'avg:aws.lambda.duration.p50 {}'.format(q_input)

More info here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.