cpcloud / ipython-autotime

Time everything in IPython
Apache License 2.0
118 stars 12 forks source link

Prevent assert exception when elapsed time is zero #12

Closed tkmktime closed 5 years ago

tkmktime commented 6 years ago

To prevent assert exception for some blank or comment cell, how do you think below code.

class LineWatcher(object):

"""Class that implements a basic timer.

Notes
-----
* Register the `start` and `stop` methods with the IPython events API.
"""

def __init__(self):
    self.start_time = 0.0

def start(self):
    self.start_time = time.time()

def stop(self):
    if self.start_time:
        diff = time.time() - self.start_time
       assert diff >= 0   # FIXED from assert diff > 0
        print('time: %s' % format_delta(diff))

timer = LineWatcher()

cpcloud commented 5 years ago

Fixed by a9c7cab