-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprogress.py
More file actions
63 lines (52 loc) · 2.18 KB
/
Copy pathprogress.py
File metadata and controls
63 lines (52 loc) · 2.18 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
# -*- coding: UTF-8 -*-
"""Module for defining progress mode logic.
"""
lazy_load_module("tqdm")
__all__ = ["set_progress_items"]
def set_progress_items(glob):
""" This function prepares the progress items for inclusion in main script's global scope.
:param glob: main script's global scope dictionary reference
"""
a = glob['args']
enabled = getattr(a, a._collisions.get("progress") or "progress", False)
# Progress manager, for providing an interface to tqdm progress bar class
class __ProgressManager(object):
""" Simple progress bar manager, relying on tqdm module. """
def __init__(self):
c = a._collisions
self._tqdm = None
def __getattr__(self, name):
if enabled:
try:
return self.__getattribute__(name)
except AttributeError:
pass
if hasattr(tqdm.tqdm, name) and self._tqdm is not None:
return getattr(self._tqdm, name)
raise AttributeError("ProgressManager instance has no attribute '{}'".format(name))
def range(self, *args, **kwargs):
""" Dummy alias to trange. """
if enabled:
self._tqdm = tqdm.trange(*args, **kwargs)
return self._tqdm
def start(self, *args, **kwargs):
if enabled:
self.stop()
self._tqdm = tqdm.tqdm(*args, **kwargs)
return self._tqdm
def stop(self):
""" Closing method. """
if enabled:
if self._tqdm is not None:
self._tqdm.close()
self._tqdm = None
glob['progress_manager'] = manager = __ProgressManager()
# shortcut function to range-based progress bar
def progressbar(*args, **kwargs):
""" Range-based progress bar relying on tqdm. """
try:
iter(args[0])
return manager.start(*args, **kwargs)
except TypeError as te:
return manager.range(*args, **kwargs)
glob['progressbar'] = progressbar