I suspect the answer to this question will be a big NO! But here goes.
Is there a way to deactivate (temporarily) long code snippets without having to comment every line or putting an if FALSE: and indenting every line?
As an example, say I have the code
for A in range(1,LargeNumber):
DoSuff(A)
###DO Mode stuff
###....
Done(A)
However, since I'm still developing the code I don't want this lengthy loop to run. My options are as far as I'm aware:
Comment away
#for A in range(1,LargeNumber):
#DoSuff(A)
####DO Mode stuff
####....
#Done(A)
or wrap in a false
if-statement
if False:
for A in range(1,LargeNumber):
DoSuff(A)
###DO Mode stuff
###....
Done(A)
both of which require me to manipulate every line I want to deactivate.
Is there some more clever way of doing this without having to change the indentation or adding comments at every line. (Except maybe putting LargeNumber=0
here.)
As requested, what I proposed :
to comment a large code :
"""
for A in range(1,LargeNumber):
DoSuff(A)
DO Mode stuff
....
Done(A)
"""
Everything in between the 2 """
will be commented. However as tobias_k said, it doesn't work well if you have multi-line strings in your code.
But, as he also said, you can make it work like this :
"""
print 'anything with sinple quotes instead of double quotes'
"""
Some suggestions:
if you have a particular long-running function you want to skip during development, you could define and use a decorator to skip that function and add it to its declaration (of course, this will only work if the function does not return anything that would be used later on)
def skip(f):
def _f(*args, **kwargs):
print("skipped function", f.__name__)
return _f
@skip
def long_running_function(foo):
print("executed function")
in case you want to skip a loop, as in your example, just add a break
statement at the top
for i in range(10):
break # TODO speed up development; remove for production!
long_running_function(i)
Personally, however, I would just go with mass-commenting the block of code you want to skip.
Here are several ways which you can use:
import
if
. That way, you have a single place in your config to enable/disable the code.If you don't have multi-line strings in the code, you can use
for A in range(1,LargeNumber):
deactivated = '''
DoSuff(A)
###DO Mode stuff
###....
Done(A)
'''
for A in range(1,LargeNumber):
break
DoSuff(A)
###DO Mode stuff
###....
Done(A)
I've generally found that using a 'pass', 'return', 'continue', or 'break' works nicely for testing...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With