-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathprogress.py
More file actions
95 lines (77 loc) · 2.73 KB
/
Copy pathprogress.py
File metadata and controls
95 lines (77 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Wrappers for progress bars. Mainly intended to use tqdm if it is available
on the user's system, but in principle, other progress bars could be used as
well.
"""
try:
from tqdm.auto import tqdm
except ImportError: # pragma: no cover
HAS_TQDM = False
else: # pragma: no cover
HAS_TQDM = True
def silent_progress(iterable, *args, **kwargs):
"""No progress output: ignores all options, and just gives the iterator
Parameters
----------
iterable : iterable
thing to iterate over
"""
return iter(iterable)
def DefaultProgress(**kwargs): # pragma: no cover
"""Factory for getting the default progress behavior.
Currently, uses tqdm if it is available; silent progress otherwise. In
the future, we may add global configuration (e.g., rcparams) to allow
users to customize this behavior.
"""
# TODO: eventually, add some sort of rcparams support for this
if HAS_TQDM:
return TqdmPartial(**kwargs)
else:
return silent_progress
class TqdmPartial(object):
"""Progress bar wrapper for tqdm-based progress meters.
Note that additional tqdm parameters can be set *after* initialization.
Parameters
----------
desc : str
label for the progress bar
leave : bool
whether to leave to progress bar visible after completion (default
True)
"""
def __init__(self, desc=None, leave=True, file=None):
self.kwargs = {'desc': desc,
'leave': leave,
'file': file}
def __getattr__(self, attr):
if attr != 'kwargs':
return self.kwargs[attr]
else:
return super(TqdmPartial, self).__getattr__(attr)
def __setattr__(self, attr, value):
if attr != 'kwargs':
self.kwargs[attr] = value
else: # pragma: no cover
super(TqdmPartial, self).__setattr__(attr, value)
def __call__(self, iterable, **kwargs):
tqdm_kwargs = self.kwargs
tqdm_kwargs = {k: v for k, v in self.kwargs.items()}
tqdm_kwargs.update(kwargs)
return tqdm(iterable, **tqdm_kwargs)
class SimpleProgress(object):
"""Mix-in for classes that need a progress meter.
Note that this will use the names ``progress`` and ``_progress``.
"""
@property
def progress(self):
if not hasattr(self, '_progress'):
self._progress = DefaultProgress()
return self._progress
@progress.setter
def progress(self, value):
if value == 'tqdm':
value = TqdmPartial() if HAS_TQDM else silent_progress
elif value in ['silent', None]:
value = silent_progress
# else we assume it's already an AnalysisProgress
self._progress = value