1

I have a little problem with my app.

Here's a code:

parameters_tuple = (case_name, x_rw[6], x_rw[0], x_rw[1], x_rw[2], x_rw[3], x_rw[6])

return """
        <testcase classname="performance_tests" name="%s" time="%s">
            <Type>%s</Type>
            <Name>%s</Name>
            <Request_Count>%s</Request_Count>
            <Failure_Count>%s</Failure_Count>
            <Min_Response_Time>%s</Min_Response_Time>
        </testcase>""" % parameters_tuple

I want to have x_rw[6] divided by 1000.

I've tried to do this like here:

parameters_tuple = (case_name, x_rw[6], x_rw[0], x_rw[1], x_rw[2], x_rw[3], x_rw[6])

return """
        <testcase classname="performance_tests" name="%s" time="%s">
            <Type>%s</Type>
            <Name>%s</Name>
            <Request_Count>%s</Request_Count>
            <Failure_Count>%s</Failure_Count>
            <Min_Response_Time>%s</Min_Response_Time>
        </testcase>""" % parameters_tuple[0], parameters_tuple[1]/1000, parameteres_tuple[2:]

but is not working at all. How to make it works?

Osho
  • 168
  • 1
  • 9

3 Answers3

1

Have you tried using f-strings? This works for Python >= 3.6

You can replace your code with:

f'<testcase classname="performance_tests" name="{parameters_tuple[0]}" time="%s">'

etc. Here, you take the value of the variables and operations inside (like it is for your division), and replace that part of the string with them.

f'<Type>"{parameters_tuple[1]/1000}"</Type>'
Bhavye Mathur
  • 1,024
  • 1
  • 7
  • 25
1

The problem with your code is that is is interpreted like this (note the (...)):

return ("""...""" % parameters_tuple[0]), parameters_tuple[1]/1000, parameters_tuple[2:]

Thus you get "not enough arguments for format string" because it tries to use only the first parameter when it needs 7. Instead, you can use (...) to create a new tuple with all the parameters to use. Use * to unpack the "rest":

return """...""" % (parameters_tuple[0], parameters_tuple[1]/1000, *parameters_tuple[2:])

However, it seems much simpler to just do the / 1000 when defining parameters_tuple:

parameters_tuple = (case_name, x_rw[6] / 1000, x_rw[0], x_rw[1], x_rw[2], x_rw[3], x_rw[6])
tobias_k
  • 81,265
  • 12
  • 120
  • 179
1
parameters_tuple = (case_name, x_rw[6] / 1000, x_rw[0], x_rw[1], x_rw[2], x_rw[3], x_rw[6])

and

f'strings helped.

Thanks guys :)

Osho
  • 168
  • 1
  • 9