Python decorators not taking a value from a constant
report.py
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description = "CHECK-ACCESS REPORTING.")
parser.add_argument('--input','-i', help='Filepath containing the Active Directory userlist')
parser.add_argument('--timestamp', '-t', nargs='?',const="BLANK", help='filepath with environement varible set')
args, unknownargs = parser.parse_known_args(sys.argv[1:])
timestampchecker(args.timestamp)
#checking the value of cons.DISPLAYRESULT is TRUE
main()
timestampchecker function:
def timestampchecker(status):
""" Check if the timestamp is to display or not from command line"""
if status is not None:
cons.DISPLAY_TIME_STAMP = True
This function checks if the -t arguments have been set by the user . If it is set, I have defined one constant called cons.DISPLAYRESULT to true.
The function works great and turns a constant value into True. But in the main function I have implemented these decorators which do not accept true value, but false
timer.py
def benchmarking(timestaus):
def wrapper(funct):
def timercheck(*args, **kwarg):
if timestaus is True:
starttime=time.time()
funct(*args, **kwarg)
if timestaus is True:
print('Time Taken:',round(time.time()-starttime, 4))
return timercheck
return wrapper
I've decorated some method in the main () method of report.py with the decorators above. For example, this is the class that is used in report.py and the decorated decorators above.
class NotAccountedReport:
def __init__(self, pluginoutputpath):
""" Path where the plugins result are stored need these files"""
self.pluginoutputpath = pluginoutputpath
@benchmarking(cons.DISPLAY_TIME_STAMP)
def makeNotAccountableReport():
#some functionality
here I have passed a constant value to the arguments decorator which, when checked, although before the call was converted to True, accepts false and thus the decorators are not executed. What is the problem I cannot understand
source to share
You haven't posted a complete minimal testable example, so there might be something else, but if your point is that when NotAccountedReport().makeNotAccountableReport()
you call you don't get a printed "Time" then this really isn't surprising - the decorator benchmarking
is applied when the function is defined (when the module is imported), long before the proposal is executed if __name__ == '__main__'
, so it was cons.DISPLAY_TIME_STAMP
not updated with your command line arguments at that time .
If you want the runtime flag to activate / deactivate the behavior of your decorator, the obvious solution is to check cons.DISPLAY_TIME_STAMP
inside the decorator instead of passing it as an argument, that is:
def benchmarking(func):
def timercheck(*args, **kwarg):
if cons.DISPLAY_TIME_STAMP:
starttime=time.time()
result = func(*args, **kwarg)
if cons.DISPLAY_TIME_STAMP:
logger.debug('Time Taken: %s',round(time.time()-starttime, 4))
return result
return timercheck
class NotAccountedReport(object):
@benchmarking
def makeNotAccountableReport():
#some functionality
source to share