经过的时间 +字典中的 datetime.timedelta

发布于 2025-01-09 22:11:53 字数 2059 浏览 1 评论 0原文

您好,感谢您的帮助:)

问题

我的问题是,为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2025-01-16 22:11:53

当您打印字典时,其中的各个键/元素将根据其 __repr__ 方法返回的值进行打印。当您直接打印对象时,将调用 __str__ 方法。

考虑这个简单的例子:

class Foo:
    def __str__(self):
        return 'foo'

    def __repr__(self):
        return 'bar'

x = Foo()
d = {'a': x}

print(x) # -> foo
print(d) # -> {'a': bar}

这个问题给出了一个很好的解释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:

class Foo:
    def __str__(self):
        return 'foo'

    def __repr__(self):
        return 'bar'

x = Foo()
d = {'a': x}

print(x) # -> foo
print(d) # -> {'a': bar}

This question gives a good explanation of the difference between str/__str__ and repr/__repr__.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文