• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yeliudev / nncore / 8692965632

15 Apr 2024 04:49PM UTC coverage: 15.676% (-0.02%) from 15.691%
8692965632

push

github

yeliudev
Fix timer flexibility

0 of 8 new or added lines in 1 file covered. (0.0%)

678 of 4325 relevant lines covered (15.68%)

3.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/nncore/utils/timer.py
1
# Copyright (c) Ye Liu. Licensed under the MIT License.
2

3
from time import perf_counter
20✔
4

5

6
class Timer(object):
20✔
7
    """
8
    A flexible timer class.
9
    """
10

11
    def __init__(self):
20✔
12
        self.reset()
×
13

14
    def reset(self):
20✔
15
        """
16
        Reset the timer.
17
        """
18
        self._start = perf_counter()
×
19
        self._paused = None
×
20
        self._paused_time = 0
×
21

22
    def is_paused(self):
20✔
23
        """
24
        Check whether the timer is paused.
25
        """
26
        return self._paused is not None
×
27

28
    def pause(self, raise_error=True):
20✔
29
        """
30
        Pause the timer.
31
        """
NEW
32
        if self.is_paused():
×
NEW
33
            if raise_error:
×
NEW
34
                raise RuntimeError('the timer that is already paused')
×
NEW
35
            return
×
36

37
        self._paused = perf_counter()
×
38

39
    def resume(self, raise_error=True):
20✔
40
        """
41
        Resume the timer.
42
        """
NEW
43
        if not self.is_paused():
×
NEW
44
            if raise_error:
×
NEW
45
                raise RuntimeError('the timer is not paused')
×
NEW
46
            return
×
47

48
        self._paused_time += perf_counter() - self._paused
×
49
        self._paused = None
×
50

51
    def seconds(self):
20✔
52
        """
53
        Return the total number of seconds since the reset of the timer,
54
        excluding the time when the timer is paused.
55
        """
56
        end_time = self._paused or perf_counter()
×
57
        return end_time - self._start - self._paused_time
×
58

59
    def minutes(self):
20✔
60
        """
61
        Return the total number of minutes since the reset of the timer,
62
        excluding the time when the timer is paused.
63
        """
64
        return self.seconds() / 60
×
65

66
    def hours(self):
20✔
67
        """
68
        Return the total number of hours since the reset of the timer,
69
        excluding the time when the timer is paused.
70
        """
71
        return self.minutes() / 60
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc