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

yeliudev / nncore / 6157025147

12 Sep 2023 08:41AM UTC coverage: 16.243% (+0.1%) from 16.134%
6157025147

push

github

yeliudev
Add support for download from url

72 of 72 new or added lines in 5 files covered. (100.0%)

664 of 4088 relevant lines covered (16.24%)

1.3 hits per line

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

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

3
from math import ceil
8✔
4
from shutil import get_terminal_size
8✔
5

6
from .timer import Timer
8✔
7

8

9
class ProgressBar(object):
8✔
10
    """
11
    A progress bar which showing the state of a progress.
12

13
    Args:
14
        iterable (iterable | None, optional): The iterable object to decorate
15
            with a progress bar. Default: ``None``.
16
        num_tasks (int | None, optional): The number of expected iterations.
17
            If not specified, the length of iterable object will be used.
18
            Default: ``None``.
19
        percentage (bool, optional): Whether to display the percentage instead
20
            of raw task numbers. Default: ``False``.
21
        active (bool | None, optional): Whether the progress bar is active. If
22
            not specified, only the main process will show the progree bar.
23
            Default: ``None``.
24

25
    Example:
26
        >>> for item in ProgressBar(range(10)):
27
        ...     # do processing
28

29
        >>> prog_bar = ProgressBar(num_tasks=10)
30
        >>> for item in range(10):
31
        ...     # do processing
32
        ...     prog_bar.update()
33
    """
34

35
    _pb = '\r[{{}}] {}, elapsed: {}, eta: {}{}'
8✔
36
    _wb = '\r[{{}}] {}/{}, {:.1f} task/s, elapsed: {}, eta: {}{}'
8✔
37
    _ob = '\rCompleted: {}, elapsed: {}, {:.1f} tasks/s'
8✔
38

39
    def __init__(self,
8✔
40
                 iterable=None,
41
                 num_tasks=None,
42
                 percentage=False,
43
                 active=None):
44
        self._iterable = iterable
×
45
        self._num_tasks = num_tasks or (len(iterable) if hasattr(
×
46
            iterable, '__len__') else None)
47
        self._percentage = percentage
×
48
        self._completed = 0
×
49

50
        if self._percentage:
×
51
            assert self._num_tasks is not None
×
52

53
        if active is None:
×
54
            try:
×
55
                from nncore.engine import is_main_process
×
56
                self._active = is_main_process()
×
57
            except ImportError:
×
58
                self._active = True
×
59
        else:
60
            self._active = active
×
61

62
        if self._active:
×
63
            if self._percentage:
×
64
                msg = self._pb.format('0%', 0, 0, '')
×
65
                msg = msg.format(' ' * self._get_bar_width(msg))
×
66
            elif self._num_tasks is not None:
×
67
                msg = self._wb.format(0, self._num_tasks, 0, 0, 0, '')
×
68
                msg = msg.format(' ' * self._get_bar_width(msg))
×
69
            else:
70
                msg = self._ob.format(0, 0, 0)
×
71

72
            print(msg, end='')
×
73
            self._last_length = len(msg)
×
74

75
            self._timer = Timer()
×
76

77
    def __iter__(self):
8✔
78
        for item in self._iterable:
×
79
            yield item
×
80
            self.update()
×
81

82
    def _get_bar_width(self, msg):
8✔
83
        width, _ = get_terminal_size()
×
84
        bar_width = min(int(width - len(msg)) + 2, int(width * 0.6), 40)
×
85
        return max(2, bar_width)
×
86

87
    def _get_time_str(self, second):
8✔
88
        if second >= 86400:
×
89
            day = second // 86400
×
90
            second -= (day * 86400)
×
91
            day = '{}d'.format(day)
×
92
        else:
93
            day = ''
×
94

95
        if second >= 3600:
×
96
            hour = second // 3600
×
97
            second -= (hour * 3600)
×
98
            hour = '{}h'.format(hour)
×
99
        else:
100
            hour = ''
×
101

102
        if second >= 60:
×
103
            minute = second // 60
×
104
            second -= minute * 60
×
105
            minute = '{}m'.format(minute)
×
106
        else:
107
            minute = ''
×
108

109
        if second > 0:
×
110
            second = '{}s'.format(second)
×
111
        else:
112
            second = ''
×
113

114
        time_str = '{}{}{}{}'.format(day, hour, minute, second) or '0s'
×
115
        return time_str
×
116

117
    def update(self, times=1):
8✔
118
        if not self._active:
×
119
            return
×
120

121
        for _ in range(times):
×
122
            self._completed += 1
×
123
            ela = self._timer.seconds()
×
124
            fps = self._completed / ela
×
125
            ela_str = self._get_time_str(ceil(ela))
×
126

127
            if self._num_tasks is not None:
×
128
                perc = self._completed / self._num_tasks
×
129
                eta = int(ela * (1 - perc) / perc + 0.5)
×
130
                eta_str = self._get_time_str(ceil(eta))
×
131
                end_str = '\n' if self._num_tasks == self._completed else ''
×
132
                if self._percentage:
×
133
                    msg = self._pb.format(f'{round(perc*100,1)}%', ela_str,
×
134
                                          eta_str, end_str)
135
                else:
136
                    msg = self._wb.format(self._completed, self._num_tasks,
×
137
                                          fps, ela_str, eta_str, end_str)
138
                bar_width = self._get_bar_width(msg)
×
139
                mark_width = int(bar_width * perc)
×
140
                chars = '>' * mark_width + ' ' * (bar_width - mark_width)
×
141
                msg = msg.format(chars)
×
142
            else:
143
                msg = self._ob.format(self._completed, ela_str, fps)
×
144

145
            print(msg.ljust(self._last_length), end='')
×
146
            self._last_length = len(msg)
×
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