A counter to count ratio of something.
| 55 | |
| 56 | |
| 57 | class RatioCounter(object): |
| 58 | """ A counter to count ratio of something. """ |
| 59 | |
| 60 | def __init__(self): |
| 61 | self.reset() |
| 62 | |
| 63 | def reset(self): |
| 64 | self._tot = 0 |
| 65 | self._cnt = 0 |
| 66 | |
| 67 | def feed(self, count, total=1): |
| 68 | """ |
| 69 | Args: |
| 70 | cnt(int): the count of some event of interest. |
| 71 | tot(int): the total number of events. |
| 72 | """ |
| 73 | self._tot += total |
| 74 | self._cnt += count |
| 75 | |
| 76 | @property |
| 77 | def ratio(self): |
| 78 | if self._tot == 0: |
| 79 | return 0 |
| 80 | return self._cnt * 1.0 / self._tot |
| 81 | |
| 82 | @property |
| 83 | def total(self): |
| 84 | """ |
| 85 | Returns: |
| 86 | int: the total |
| 87 | """ |
| 88 | return self._tot |
| 89 | |
| 90 | @property |
| 91 | def count(self): |
| 92 | """ |
| 93 | Returns: |
| 94 | int: the total |
| 95 | """ |
| 96 | return self._cnt |
| 97 | |
| 98 | |
| 99 | class Accuracy(RatioCounter): |
no outgoing calls
no test coverage detected