经过的时间 +字典中的 datetime.timedelta
您好,感谢您的帮助:)
问题
我的问题是,为什么 task.print_times()
正确打印经过的时间...
2022-02-24 21:35:23
2022-02-24 21:35:26
0:00:03
但是 task.dict_times()
打印这个...
{
'time_start': '2022-02-24 21:35:23',
'time_end': '2022-02-24 21:35:26',
'time_elapsed': datetime.timedelta(seconds=3)
}
代码
from datetime import timedelta
from timeit import default_timer as timer
from time import strftime, localtime, sleep
class Timer:
'''A utility class for capturing a task's processing time.
'''
def __init__(self):
self.start_timer = None
self.time_start = None
self.stop_timer = None
self.time_stop = None
self.time_elapsed = None
def start(self):
'''Start the timer and capture the start time.
'''
self.start_timer = timer()
self.time_start = strftime("%Y-%m-%d %H:%M:%S", localtime())
def stop(self):
'''Stop the timer and capture the stop time.
'''
self.stop_timer = timer()
self.time_stop = strftime("%Y-%m-%d %H:%M:%S", localtime())
def elapsed(self):
'''Calculates the elapsed time.
'''
elapsed = timedelta(seconds=self.stop_timer-self.start_timer)
self.time_elapsed = elapsed - timedelta(microseconds=elapsed.microseconds)
# --------------------------
# Example usage
# --------------------------
class Task:
def __init__(self):
self.task_timer = Timer()
self.time_summary = {}
def do(self):
self.task_timer.start()
sleep(3)
self.task_timer.stop()
self.task_timer.elapsed()
def print_times(self):
print(self.task_timer.time_start)
print(self.task_timer.time_stop)
print(self.task_timer.time_elapsed)
def dict_times(self):
self.time_summary['time_start'] = self.task_timer.time_start
self.time_summary['time_end'] = self.task_timer.time_stop
self.time_summary['time_elapsed'] = self.task_timer.time_elapsed
print(self.time_summary)
task = Task()
task.do()
task.print_times()
task.dict_times()
Hello and thanks for your help :)
Question
My question is, why does task.print_times()
prints the elapsed time correctly...
2022-02-24 21:35:23
2022-02-24 21:35:26
0:00:03
But task.dict_times()
prints this...
{
'time_start': '2022-02-24 21:35:23',
'time_end': '2022-02-24 21:35:26',
'time_elapsed': datetime.timedelta(seconds=3)
}
Code
from datetime import timedelta
from timeit import default_timer as timer
from time import strftime, localtime, sleep
class Timer:
'''A utility class for capturing a task's processing time.
'''
def __init__(self):
self.start_timer = None
self.time_start = None
self.stop_timer = None
self.time_stop = None
self.time_elapsed = None
def start(self):
'''Start the timer and capture the start time.
'''
self.start_timer = timer()
self.time_start = strftime("%Y-%m-%d %H:%M:%S", localtime())
def stop(self):
'''Stop the timer and capture the stop time.
'''
self.stop_timer = timer()
self.time_stop = strftime("%Y-%m-%d %H:%M:%S", localtime())
def elapsed(self):
'''Calculates the elapsed time.
'''
elapsed = timedelta(seconds=self.stop_timer-self.start_timer)
self.time_elapsed = elapsed - timedelta(microseconds=elapsed.microseconds)
# --------------------------
# Example usage
# --------------------------
class Task:
def __init__(self):
self.task_timer = Timer()
self.time_summary = {}
def do(self):
self.task_timer.start()
sleep(3)
self.task_timer.stop()
self.task_timer.elapsed()
def print_times(self):
print(self.task_timer.time_start)
print(self.task_timer.time_stop)
print(self.task_timer.time_elapsed)
def dict_times(self):
self.time_summary['time_start'] = self.task_timer.time_start
self.time_summary['time_end'] = self.task_timer.time_stop
self.time_summary['time_elapsed'] = self.task_timer.time_elapsed
print(self.time_summary)
task = Task()
task.do()
task.print_times()
task.dict_times()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您打印字典时,其中的各个键/元素将根据其
__repr__
方法返回的值进行打印。当您直接打印对象时,将调用__str__
方法。考虑这个简单的例子:
这个问题给出了一个很好的解释
str
/__str__
和repr
/__repr__
之间的区别。When you print a dictionary, the individual keys/elements within are printed according to the value returned by their
__repr__
methods. When you print an object directly, the__str__
method is invoked instead.Consider this simple example:
This question gives a good explanation of the difference between
str
/__str__
andrepr
/__repr__
.