Skip to content
Advertisement

How to timeit.timeit multi-line output without EOL error?

I have the following code stored in a .py file:

JavaScript

This code executes perfectly fine. But now I want to measure the time it takes to execute the code. So I try to insert it into timeit.timeit in the following way:

JavaScript

And now I get the following error:

JavaScript

So apparently, it seems like the python code is giving me EOL error because I put n before the final quote. But in the problem I’m trying to solve, I really need to have multiple lines (including blank lines) in a specific pattern, as part of the final output, and it needs to create those blank lines automatically for a large variety of multi-line inputs.

The input1 is a modified version of the actual input just so I can see more clearly what’s going on while coding. The actual input will be the same as input1, except that it’s a file object rather than a list, and there will be a new line instead of the commas. My code will convert that file object into the input1 list that you see in the code.

So how do I timeit.timeit the multi-line output from above code without EOL error?

Advertisement

Answer

you can call a function with timeit

JavaScript

however your actual problem is just malformed code … it opens a string and never closes it

you need to escape"n" to "\n", otherwise your n gets evaluated literally to

JavaScript

which is a syntax error of unterminated string literal

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement