【发布时间】:2017-12-24 16:32:02
【问题描述】:
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()
时间戳检查功能:
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
此函数检查用户是否设置了 -t 参数。如果已设置,我已将一个名为 cons.DISPLAYRESULT 的常量定义为 true。
该函数运行良好,并将常量值变为 True。 但是在主要功能中,我已经实现了这个装饰器,它不是取真值而是假的
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
我在report.py 的main() 方法中用上面的装饰器装饰了一些方法。例如,这是在 report.py 中使用并使用上述装饰器进行装饰的类
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
这里我将常量值传递给参数装饰器 测试时,虽然在调用之前转换为 True 是假的 因此装饰器没有被实现。问题出在哪里 想不通
【问题讨论】:
-
这是一个微不足道的观点,但也许我不理解你的最后一段。当您说“我已将常量值传递给参数装饰器”时,您的意思是
@benchmarking(const)?那么const is True和bool(const) is True会有不同的结果。 -
我的意思是说我已经定义了常量并且那个值是装饰器被传递的。装饰器仅在该值设置为
True时运行。但是正在发生的事情是认为常量值更改为 True 装饰器首先被加载并取而代之的是False
标签: python python-2.7 python-decorators