Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python decorators not taking the value from constant passed

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 user have set the -t arguments. If it is set I have defined one constant called cons.DISPLAYRESULT to true.

The function is working great and turning the constant value to True. But in the main function I have implemented this decorators which is not taking the 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 have decorated some method in main() method of report.py with the decorators above. For example This is the class being used in report.py and being decorated with above decorators

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 the constant value to the argument decorator which when tested though before called is converted to True is taking false and thus the decorators not being implemented. Where is the problem cant figure out

like image 906
Tara Prasad Gurung Avatar asked Sep 09 '26 05:09

Tara Prasad Gurung


1 Answers

You didn't post a complete minimal verifiable example so there might be something else too, but if your point is that when calling NotAccountedReport().makeNotAccountableReport() you don't get your "Time taken" printed then it's really not a surprise - the benchmarking decorator is applied when the function is defined (when the module is imported), well before the if __name__ == '__main__' clause is executed, so at that time cons.DISPLAY_TIME_STAMP has not been updated by your command line args.

If you want a runtime flag to activate / deactivate your decorator's behaviour the obvious solution is to check cons.DISPLAY_TIME_STAMP within the decorator instead of passing it as argument, ie:

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
like image 177
bruno desthuilliers Avatar answered Sep 10 '26 19:09

bruno desthuilliers