I have created verbose_on (it shows output if used) and verbose_off fuction. In verbose_on(fn), fn is another function.
helper.py
import os
import sys
import io
def verbose_on(fn):
sys.stdout = io.StringIO()
fn()
output = sys.stdout.getvalue()
sys.stdout = sys.__stdout__
print(output)
def verbose_off(fn):
sys.stdout = io.StringIO()
fn()
sys.stdout = sys.__stdout__
print('Task Completed.')
/dev.py
In sample function there are some commands, whose output need to be showed in test function if verbose is on.
def sample(flag2):
print('tests are running')
# another commands running
/main.py
It has test function which has two flags, one to activate verbose and another flag which is needed to pass with sample. Check helper.verbose_off(sample) in test function, it is passing sample which will be converted to function in verbose_on method(fn(), i.e. sample())
from dev import sample
from helper import verbose_on, verbose_off
def test(verbose, flag2):
if verbose:
print('Verbose mode = On.')
helper.verbose_on(sample)
else:
print('Verbose mode = Off')
helper.verbose_off(sample)
But it is needed that flag2 shoud be pass with sample inside test function, something like this : helper.verbose_on(sample(flag2)). Any suggestions will be appreciated. Thank you in advance.
lambda x: sample(flag)def verbose_on(fn, *args, **kwargs), that callsfn(*args, **kwargs), and you callverbose_on(sample, flag2)