Python 2.7 - 使用 类 作为时间和记忆的装饰器
Python 2.7 - Using Classes as Decorators for Timing and Memoization
我是 Python 的新手,我目前正在做一个项目,我有一个 Timer 和 Memoize class,它们都应该能够用作装饰器并且可以使用具有任意数量参数的函数。
问题
我目前的问题是我试图将它们都用作函数的装饰器;但是,Timer 仅在第一次调用该函数时被调用,而不是第二次调用。例如,使用此代码:
# Import the Memoize class from the memoization module
from memoization import Memoize
# Import the time module
import time
# Import the logging module
import logging
# Import the Timer class from the timer module
from timer import Timer
@Memoize
@Timer
def pass_and_square_time(seconds):
# Call time.sleep(seconds)
time.sleep(seconds)
# Return the square of the input seconds amount.
return seconds**2
def main():
logging.getLogger().setLevel(logging.ERROR)
print '\nFor pass_and_square_time({30}):'.format(n=num)
print '\n\tThe initial call of pass_and_square_time(30) yields: {ret}'.format(ret=pass_and_square_time(30))
print '\n\tThe second call of pass_and_square_time(30) yields: {ret}'.format(ret=pass_and_square_time(30))
return如下:
For pass_and_square_time(30):
<function pass_and_square_time at 0x02B9A870> 30.003000021 seconds
The initial call of pass_and_square_time(30) yields: 900
The second call of pass_and_square_time(30) yields: 900
当我想要它时 return 第二次调用之前的秒数(因为那是第二个时间。初始调用之上的时间是初始调用的时间)。我相信 @Memoize 装饰器在第二次调用时正常工作,因为它几乎在第一次调用之后显示,而不是执行 time.sleep(30) 调用。
定时器
我的定时器class实现如下:
class Timer(object):
def __init__(self, fcn, timer_name='Timer'):
self._start_time = None
self._last_timer_result = None
self._display = 'seconds'
self._fcn = fcn
self._timer_name = timer_name
self.__wrapped__ = self._fcn
def __call__(self, *args):
self.start()
fcn_res = self._fcn(*args)
self.end()
print '\n{func} {time} seconds'.format(func=self._fcn, time=self.last_timer_result)
return fcn_res
'''
start(), end(), and last_timer_result functions/properties implemented
below in order to set the start_time, set the end_time and calculate the
last_timer_result, and return the last_timer_result. I can include more
if you need it. I didn't include it just because I didn't want to make
the post too long
'''
记忆
我的Memoizeclass实现如下:
class Memoize(object):
def __init__(self, fcn):
self._fcn = fcn
self._memo = {}
self.__wrapped__ = self.__call__
def __call__(self, *args):
if args not in self._memo:
self._memo[args] = self._fcn(*args)
return self._memo[args]
使用的参考资料
我查看并试图模仿我的 class 的参考文献或者是:
Python Class 装饰器
- https://krzysztofzuraw.com/blog/2016/python-class-decorators.html
- Python decorator best practice, using a class vs a function
Python 记忆
感谢您阅读并提供任何帮助!
使用您的代码,您正在记忆一个定时函数,这意味着当在缓存中找到参数时,也会跳过定时代码。如果你颠倒装饰器的顺序
@Timer
@Memoize
def pass_and_square_time(seconds):
# Call time.sleep(seconds)
time.sleep(seconds)
# Return the square of the input seconds amount.
return seconds**2
现在您正在为记忆函数计时。无论是否在缓存中找到参数,您都可以对记忆函数的调用进行计时。
我是 Python 的新手,我目前正在做一个项目,我有一个 Timer 和 Memoize class,它们都应该能够用作装饰器并且可以使用具有任意数量参数的函数。
问题
我目前的问题是我试图将它们都用作函数的装饰器;但是,Timer 仅在第一次调用该函数时被调用,而不是第二次调用。例如,使用此代码:
# Import the Memoize class from the memoization module
from memoization import Memoize
# Import the time module
import time
# Import the logging module
import logging
# Import the Timer class from the timer module
from timer import Timer
@Memoize
@Timer
def pass_and_square_time(seconds):
# Call time.sleep(seconds)
time.sleep(seconds)
# Return the square of the input seconds amount.
return seconds**2
def main():
logging.getLogger().setLevel(logging.ERROR)
print '\nFor pass_and_square_time({30}):'.format(n=num)
print '\n\tThe initial call of pass_and_square_time(30) yields: {ret}'.format(ret=pass_and_square_time(30))
print '\n\tThe second call of pass_and_square_time(30) yields: {ret}'.format(ret=pass_and_square_time(30))
return如下:
For pass_and_square_time(30):
<function pass_and_square_time at 0x02B9A870> 30.003000021 seconds
The initial call of pass_and_square_time(30) yields: 900
The second call of pass_and_square_time(30) yields: 900
当我想要它时 return 第二次调用之前的秒数(因为那是第二个时间。初始调用之上的时间是初始调用的时间)。我相信 @Memoize 装饰器在第二次调用时正常工作,因为它几乎在第一次调用之后显示,而不是执行 time.sleep(30) 调用。
定时器
我的定时器class实现如下:
class Timer(object):
def __init__(self, fcn, timer_name='Timer'):
self._start_time = None
self._last_timer_result = None
self._display = 'seconds'
self._fcn = fcn
self._timer_name = timer_name
self.__wrapped__ = self._fcn
def __call__(self, *args):
self.start()
fcn_res = self._fcn(*args)
self.end()
print '\n{func} {time} seconds'.format(func=self._fcn, time=self.last_timer_result)
return fcn_res
'''
start(), end(), and last_timer_result functions/properties implemented
below in order to set the start_time, set the end_time and calculate the
last_timer_result, and return the last_timer_result. I can include more
if you need it. I didn't include it just because I didn't want to make
the post too long
'''
记忆
我的Memoizeclass实现如下:
class Memoize(object):
def __init__(self, fcn):
self._fcn = fcn
self._memo = {}
self.__wrapped__ = self.__call__
def __call__(self, *args):
if args not in self._memo:
self._memo[args] = self._fcn(*args)
return self._memo[args]
使用的参考资料
我查看并试图模仿我的 class 的参考文献或者是:
Python Class 装饰器
- https://krzysztofzuraw.com/blog/2016/python-class-decorators.html
- Python decorator best practice, using a class vs a function
Python 记忆
感谢您阅读并提供任何帮助!
使用您的代码,您正在记忆一个定时函数,这意味着当在缓存中找到参数时,也会跳过定时代码。如果你颠倒装饰器的顺序
@Timer
@Memoize
def pass_and_square_time(seconds):
# Call time.sleep(seconds)
time.sleep(seconds)
# Return the square of the input seconds amount.
return seconds**2
现在您正在为记忆函数计时。无论是否在缓存中找到参数,您都可以对记忆函数的调用进行计时。