Remove logic from update of base classes, so subclasses don't have to call super

This commit is contained in:
Giorgos Verigakis
2012-04-20 18:09:29 +03:00
parent 65471d7609
commit 32afe021ea
4 changed files with 12 additions and 12 deletions

View File

@@ -46,17 +46,19 @@ class Infinite(object):
w = self.avg_window
self.avg = dt if self.avg is None else (dt + w * self.avg) / (w + 1)
def update(self):
self.update_stats()
kv = [(key, val) for key, val in self.__dict__.items()
if not key.startswith('_')]
self.ctx.update(kv)
def update(self):
pass
def finish(self):
pass
def next(self):
self.index = self.index + 1
self.update_stats()
self.update()
def iter(self, it):
@@ -75,9 +77,6 @@ class Progress(Infinite):
self.eta = None
def update_stats(self):
if self.delta <= 0:
return
# Calculate moving average
now = time()
dt = (now - self._ts) / self.delta
@@ -90,10 +89,16 @@ class Progress(Infinite):
self.remaining = self.max - self.index
self.eta = int(ceil(self.avg * self.remaining))
kv = [(key, val) for key, val in self.__dict__.items()
if not key.startswith('_')]
self.ctx.update(kv)
def next(self):
prev = self.index
self.index = min(self.index + 1, self.max)
self.delta = self.index - prev
if self.delta > 0:
self.update_stats()
self.update()
def goto(self, index):
@@ -104,6 +109,8 @@ class Progress(Infinite):
self.index = index
self.delta = delta
if delta > 0:
self.update_stats()
self.update()
def iter(self, it):

View File

@@ -28,7 +28,6 @@ class Bar(WritelnMixin, Progress):
fill = '#'
def update(self):
super(Bar, self).update()
filled_length = int(self.width * self.progress)
empty_length = self.width - filled_length
@@ -63,8 +62,6 @@ class IncrementalBar(Bar):
phases = (u' ', u'', u'', u'', u'', u'', u'', u'', u'')
def update(self):
super(IncrementalBar, self).update()
nphases = len(self.phases)
expanded_length = int(nphases * self.width * self.progress)
filled_length = int(self.width * self.progress)

View File

@@ -22,13 +22,11 @@ class Counter(WriteMixin, Infinite):
message = ''
def update(self):
super(Counter, self).update()
self.write(str(self.index))
class Countdown(WriteMixin, Progress):
def update(self):
super(Countdown, self).update()
self.write(str(self.remaining))
@@ -36,7 +34,6 @@ class Stack(WriteMixin, Progress):
phases = (u' ', u'', u'', u'', u'', u'', u'', u'', u'')
def update(self):
super(Stack, self).update()
nphases = len(self.phases)
i = min(nphases - 1, int(self.progress * nphases))
self.write(self.phases[i])

View File

@@ -23,7 +23,6 @@ class Spinner(WriteMixin, Infinite):
phases = ('-', '\\', '|', '/')
def update(self):
super(Spinner, self).update()
i = self.index % len(self.phases)
self.write(self.phases[i])