Skip to content

Commit

Permalink
ProgressBar -> generator
Browse files Browse the repository at this point in the history
  • Loading branch information
fx5 committed Dec 6, 2012
1 parent 93d635a commit ada0cc0
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@

class ProgressBar(object):
"""Just a Progress Bar"""
def __init__(self):

def __init__(self, x_res=80, y_res=31):
self.pbar = self.draw_bar()
self.x_res = x_res
self.y_res = y_res
self.len = (x_res + 1) * y_res
self.pos = 0

def draw_bar(self):
def m(c):
z = c
for i in xrange(200):
z = z ** 2 + c
if abs(z.real) > 3:
return i
return 0
c1 = complex(-2.2,-0.9)
c1 = complex(-2.2, -0.9)
c2 = complex(0.6, 0.9)
out = []
x_res, y_res = 80, 31
x_res, y_res = self.x_res, self.y_res
for i in xrange(y_res):
y = c1.imag + i * (c2.imag - c1.imag) / float(y_res - 1)
for j in xrange(x_res):
x = c1.real + j * (c2.real - c1.real) / float(x_res - 1)
v = m(complex(x,y))
v = m(complex(x, y))
if v:
v = v % 20
out.append(chr(ord("A")+v))
yield chr(ord("A") + v)
else:
out.append(".")
out.append(os.linesep)
self.bar = "".join(out)
self.pos = 0
yield "."
yield os.linesep

def progress(self, v):
v = min(v, 1)
new_pos = int(len(self.bar) * v)
sys.stdout.write(self.bar[self.pos:new_pos])
new_pos = int(self.len * v)
sys.stdout.write("".join(self.pbar.next()
for i in xrange(new_pos - self.pos)))
sys.stdout.flush()
self.pos = new_pos

Expand Down

0 comments on commit ada0cc0

Please sign in to comment.