| 25 | |
| 26 | |
| 27 | class Infinite(object): |
| 28 | file = stderr |
| 29 | sma_window = 10 # Simple Moving Average window |
| 30 | |
| 31 | def __init__(self, *args, **kwargs): |
| 32 | self.index = 0 |
| 33 | self.start_ts = time() |
| 34 | self.avg = 0 |
| 35 | self._ts = self.start_ts |
| 36 | self._xput = deque(maxlen=self.sma_window) |
| 37 | for key, val in kwargs.items(): |
| 38 | setattr(self, key, val) |
| 39 | |
| 40 | def __getitem__(self, key): |
| 41 | if key.startswith('_'): |
| 42 | return None |
| 43 | return getattr(self, key, None) |
| 44 | |
| 45 | @property |
| 46 | def elapsed(self): |
| 47 | return int(time() - self.start_ts) |
| 48 | |
| 49 | @property |
| 50 | def elapsed_td(self): |
| 51 | return timedelta(seconds=self.elapsed) |
| 52 | |
| 53 | def update_avg(self, n, dt): |
| 54 | if n > 0: |
| 55 | self._xput.append(dt / n) |
| 56 | self.avg = sum(self._xput) / len(self._xput) |
| 57 | |
| 58 | def update(self): |
| 59 | pass |
| 60 | |
| 61 | def start(self): |
| 62 | pass |
| 63 | |
| 64 | def finish(self): |
| 65 | pass |
| 66 | |
| 67 | def next(self, n=1): |
| 68 | now = time() |
| 69 | dt = now - self._ts |
| 70 | self.update_avg(n, dt) |
| 71 | self._ts = now |
| 72 | self.index = self.index + n |
| 73 | self.update() |
| 74 | |
| 75 | def iter(self, it): |
| 76 | try: |
| 77 | for x in it: |
| 78 | yield x |
| 79 | self.next() |
| 80 | finally: |
| 81 | self.finish() |
| 82 | |
| 83 | |
| 84 | class Progress(Infinite): |
nothing calls this directly
no outgoing calls
no test coverage detected