我正在用Python重写一个C#Console应用程序,我想要移植一个我写的不确定的基于控制台的进度条类。
我有使用文本创建确定进度条的示例,但我不确定如何做一个不确定的进度条。我假设我需要某种线程。谢谢你的帮助!
这是班级:
public class Progress {
String _status = "";
Thread t = null;
public Progress(String status) {
_status = status;
}
public Progress Start() {
t = new Thread(() => {
Console.Write(_status + " ");
while (true) {
Thread.Sleep(300);
Console.Write("\r" + _status + " ");
Thread.Sleep(300);
Console.Write("\r" + _status + " . ");
Thread.Sleep(300);
Console.Write("\r" + _status + " .. ");
Thread.Sleep(300);
Console.Write("\r" + _status + " ...");
}
});
t.Start();
return this;
}
public void Stop(Boolean appendLine = false) {
t.Abort();
Console.Write("\r" + _status + " ... ");
if (appendLine)
Console.WriteLine();
}
}
( P.S。随意参加Progress课程)
答案 0 :(得分:3)
import sys, time
while True:
for i in range( 4 ):
sys.stdout.write( '\r' + ( '.' * i ) + ' ' )
sys.stdout.flush()
time.sleep( 0.5 )
在命令行上执行动画。这里的Python中应该有足够的线程示例。
线程可能的解决方案;不知道写一个真正的线程是否会更有效率,因为我没有使用python多线程.. 来自线程导入计时器 import sys,time
def animation ( i = 0 ):
sys.stdout.write( '\r' + ( '.' * i ) + ' ' )
sys.stdout.flush()
Timer( 0.5, animation, ( 0 if i == 3 else i + 1, ) ).start()
animation()
print( 'started!' )
while True:
pass
答案 1 :(得分:1)
我在Python中实现了类似的东西。看看这个,并根据您的需要进行修改:
class ProgressBar(object):
def __init__(self, min_val=0, max_val=100, width=30, stdout=sys.stdout):
self._progress_bar = '[]' # holds the progress bar string
self._old_progress_bar = '[]'
self.min = min_val
self.max = max_val
self.span = max_val - min_val
self.width = width
self.current = 0 # holds current progress value
self.stdout = stdout
self.update(min_val) # builds the progress bar string
def increment(self, incr):
self.update(self.current + incr)
def update(self, val):
"""Rebuild the progress bar string with the given progress
value as reference.
"""
# cap the value at [min, max]
if val < self.min: val = self.min
if val > self.max: val = self.max
self.current = val
# calculate percentage done
diff = self.current - self.min
done = int(round((float(diff) / float(self.span)) * 100.0))
# calculate corresponding number of filled spaces
full = self.width - 2
filled = int(round((done / 100.0) * full))
# build the bar
self._progress_bar = '[%s>%s] %d%%' % \
('=' * (filled - 1), ' ' * (full - filled), done)
def draw(self, padding=0):
"""Draw the progress bar to current line in stdout.
"""
if self._old_progress_bar != self._progress_bar:
self._old_progress_bar = self._progress_bar
self.stdout.write('\r%s%s ' %
(' ' * padding, self._progress_bar))
self.stdout.flush() # force stdout update
def close(self):
"""Finish the progress bar. Append a newline and close
stdout handle.
"""
self.stdout.write('\n')
self.stdout.flush()
def __str__(self):
return self._progress_bar
样本用法:
def reporter(count, size, total):
"""The reporthook callback."""
if self._progbar is None:
self._progbar = ProgressBar(max_val=total)
self._progbar.increment(size)
self._progbar.draw(padding=3)
try:
message = 'Begin downloading %s to %s' % (self.url, self.to)
LOGGER.debug(message)
print message
filename, headers = urllib.urlretrieve(self.url, self.to, reporter)
print 'Download finished.'
except:
LOGGER.exception('Download interrupted: %s' % sys.exc_info()[0])
finally:
self._progbar.close()