-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker.py
94 lines (82 loc) · 2.31 KB
/
ticker.py
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
# -*- coding: iso-8859-1 -*-
# ticker.py
# Progress indicator class
# Copyright 2006 Giuseppe Venturini
# This file is part of the ahkab simulator.
#
# Ahkab is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# Ahkab is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License v2
# along with ahkab. If not, see <http://www.gnu.org/licenses/>.
"""
Implements a progress indicator.
"""
import sys
class ticker:
"""This is a progress indicator class.
If activated, you shouldn't print anything to screen before calling
ticker.hide().
If you wish to change the progress indicator, change self.progress to
something else.
"""
progress = ("-", "\\", "|", "/")
_index = 0
_step = 0
_display = False
increments_for_step = 1
def __init__(self, increments_for_step=10):
self.increments_for_step = increments_for_step
def step(self, enable):
"""After calling this function ticker.increments_for_step times
the status is incremented."""
if not enable:
return
if (self._index + 1) % self.increments_for_step == 0:
if (self._step + 1) % len(self.progress) == 0:
self._step = 0
else:
self._step = self._step + 1
self._index = 0
if self._display:
sys.stdout.write("\b"+self.progress[self._step])
sys.stdout.flush()
else:
self._index = self._index + 1
def hide(self, enable):
"""Before printing to screen, call this to hide the progress
indicator.
"""
if not enable:
return
sys.stdout.write("\b")
sys.stdout.flush()
self._display = False
def display(self, enable):
"""Print to screen the progress indicator. Call hide to hide it
again.
"""
if not enable:
return
sys.stdout.write(self.progress[self._step])
sys.stdout.flush()
self._display = True
def reset(self):
"""Reset to initial status. Doesn't hide it."""
self._step = 0
self._index = 0
if __name__ == "__main__":
# test
import time
tk = ticker()
tk.display()
for i in range(100000):
time.sleep(0.0001)
tk.step()
tk.hide()