~dosage-dev/dosage/bunch-of-comics

« back to all changes in this revision

Viewing changes to dosage/progress.py

  • Committer: ns
  • Date: 2009-12-01 07:00:22 UTC
  • Revision ID: ns@ww1aviationlinks.cjb.net-20091201070022-qkhn13d7xsoi35eu
repair TheWhiteboard by removing indirectStarter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import division
2
 
 
3
 
import sys
4
 
import time
5
 
 
6
 
from dosage import util
7
 
 
8
 
class Guess(object):
9
 
    def __init__(self, weight):
10
 
        self.weight = weight
11
 
        self.guess = 0
12
 
        self.best = 0
13
 
 
14
 
    def feed(self, value):
15
 
        self.guess = self.weight * value + (1 - self.weight) * self.guess
16
 
 
17
 
    def distance(self, value):
18
 
        return (self.guess - value) ** 2
19
 
 
20
 
class FortuneTeller(object):
21
 
    weights = (0.2, 0.3, 0.4)
22
 
 
23
 
    def __init__(self):
24
 
        self.guesses = map(Guess, self.weights)
25
 
 
26
 
    def feed(self, value):
27
 
        best = min([(guess.distance(value), guess) for guess in self.guesses])[1]
28
 
        best.best += 1
29
 
        for guess in self.guesses:
30
 
            guess.feed(value)
31
 
 
32
 
    def predict(self):
33
 
        return max([(guess.best, guess) for guess in self.guesses])[1].guess
34
 
 
35
 
class OperationComplete(Exception): pass
36
 
 
37
 
def drawBar(fill, total, caption):
38
 
    screenWidth = util.getWindowSize()
39
 
    ratio = fill / total
40
 
    mask = '[%%s>%%s] (%.2f%%%%) %s' % (ratio * 100, caption)
41
 
 
42
 
    barWidth = screenWidth - len(mask) + 6
43
 
    fillWidth = int(barWidth * ratio) - 1
44
 
    emptyWidth = barWidth - fillWidth - 1
45
 
 
46
 
    sys.stdout.write('\r')
47
 
    sys.stdout.write(mask % ('=' * fillWidth, '-' * emptyWidth))
48
 
    sys.stdout.flush()
49
 
 
50
 
def drawBounceBar(pos, caption):
51
 
    screenWidth = util.getWindowSize()
52
 
    mask = '[%%s<=>%%s] %s' % (caption,)
53
 
 
54
 
    barWidth = screenWidth - len(mask) + 4
55
 
    leftWidth = pos % barWidth - 1
56
 
    rightWidth = barWidth - leftWidth - 1
57
 
 
58
 
    sys.stdout.write('\r')
59
 
    sys.stdout.write(mask % (' ' * leftWidth, ' ' * rightWidth))
60
 
    sys.stdout.flush()
61
 
 
62
 
def progressBar(fn):
63
 
    completed = bps = 0
64
 
    count = 0
65
 
    ft = FortuneTeller()
66
 
    currentTime = lastTime = time.time()
67
 
    try:
68
 
        while 1:
69
 
            inc = 0
70
 
            while currentTime - lastTime < 0.2:
71
 
                progress, total = fn()
72
 
                inc += progress
73
 
                currentTime = time.time()
74
 
 
75
 
            ft.feed(inc / (currentTime - lastTime))
76
 
            lastTime = currentTime
77
 
 
78
 
            completed += inc
79
 
            bps = ft.predict()
80
 
 
81
 
            if total == 0:
82
 
                drawBounceBar(count, '%s/sec' % util.saneDataSize(bps))
83
 
                count += 1
84
 
            else:
85
 
                drawBar(completed, max(total, completed), '%s/sec' % util.saneDataSize(bps))
86
 
    except OperationComplete:
87
 
        if count > 0:
88
 
            drawBounceBar(count, '%s/sec' % util.saneDataSize(bps))
89
 
        else:
90
 
            drawBar(max(total, completed), max(total, completed), '%s/sec' % util.saneDataSize(bps))
91
 
    print ''