| Total Complexity | 40 |
| Total Lines | 156 |
| Duplicated Lines | 20.51 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Stats often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from __future__ import division |
||
| 14 | class Stats(object): |
||
| 15 | fields = ( |
||
| 16 | "min", "max", "mean", "stddev", "rounds", "median", "iqr", "q1", "q3", "iqr_outliers", "stddev_outliers", |
||
| 17 | "outliers", "ld15iqr", "hd15iqr", "ops", "total" |
||
| 18 | ) |
||
| 19 | |||
| 20 | def __init__(self): |
||
| 21 | self.data = [] |
||
| 22 | |||
| 23 | def __bool__(self): |
||
| 24 | return bool(self.data) |
||
| 25 | |||
| 26 | def __nonzero__(self): |
||
| 27 | return bool(self.data) |
||
| 28 | |||
| 29 | def as_dict(self): |
||
| 30 | return dict( |
||
| 31 | (field, getattr(self, field)) |
||
| 32 | for field in self.fields |
||
| 33 | ) |
||
| 34 | |||
| 35 | def update(self, duration): |
||
| 36 | self.data.append(duration) |
||
| 37 | |||
| 38 | @cached_property |
||
| 39 | def sorted_data(self): |
||
| 40 | return sorted(self.data) |
||
| 41 | |||
| 42 | @cached_property |
||
| 43 | def total(self): |
||
| 44 | return sum(self.data) |
||
| 45 | |||
| 46 | @cached_property |
||
| 47 | def min(self): |
||
| 48 | return min(self.data) |
||
| 49 | |||
| 50 | @cached_property |
||
| 51 | def max(self): |
||
| 52 | return max(self.data) |
||
| 53 | |||
| 54 | @cached_property |
||
| 55 | def mean(self): |
||
| 56 | return statistics.mean(self.data) |
||
| 57 | |||
| 58 | @cached_property |
||
| 59 | def stddev(self): |
||
| 60 | if len(self.data) > 1: |
||
| 61 | return statistics.stdev(self.data) |
||
| 62 | else: |
||
| 63 | return 0 |
||
| 64 | |||
| 65 | @property |
||
| 66 | def stddev_outliers(self): |
||
| 67 | """ |
||
| 68 | Count of StdDev outliers: what's beyond (Mean - StdDev, Mean - StdDev) |
||
| 69 | """ |
||
| 70 | count = 0 |
||
| 71 | q0 = self.mean - self.stddev |
||
| 72 | q4 = self.mean + self.stddev |
||
| 73 | for val in self.data: |
||
| 74 | if val < q0 or val > q4: |
||
| 75 | count += 1 |
||
| 76 | return count |
||
| 77 | |||
| 78 | @cached_property |
||
| 79 | def rounds(self): |
||
| 80 | return len(self.data) |
||
| 81 | |||
| 82 | @cached_property |
||
| 83 | def median(self): |
||
| 84 | return statistics.median(self.data) |
||
| 85 | |||
| 86 | @cached_property |
||
| 87 | def ld15iqr(self): |
||
| 88 | """ |
||
| 89 | Tukey-style Lowest Datum within 1.5 IQR under Q1. |
||
| 90 | """ |
||
| 91 | if len(self.data) == 1: |
||
| 92 | return self.data[0] |
||
| 93 | else: |
||
| 94 | return self.sorted_data[bisect_left(self.sorted_data, self.q1 - 1.5 * self.iqr)] |
||
| 95 | |||
| 96 | @cached_property |
||
| 97 | def hd15iqr(self): |
||
| 98 | """ |
||
| 99 | Tukey-style Highest Datum within 1.5 IQR over Q3. |
||
| 100 | """ |
||
| 101 | if len(self.data) == 1: |
||
| 102 | return self.data[0] |
||
| 103 | else: |
||
| 104 | pos = bisect_right(self.sorted_data, self.q3 + 1.5 * self.iqr) |
||
| 105 | if pos == len(self.data): |
||
| 106 | return self.sorted_data[-1] |
||
| 107 | else: |
||
| 108 | return self.sorted_data[pos] |
||
| 109 | |||
| 110 | View Code Duplication | @cached_property |
|
|
|
|||
| 111 | def q1(self): |
||
| 112 | rounds = self.rounds |
||
| 113 | data = self.sorted_data |
||
| 114 | |||
| 115 | # See: https://en.wikipedia.org/wiki/Quartile#Computing_methods |
||
| 116 | if rounds == 1: |
||
| 117 | return data[0] |
||
| 118 | elif rounds % 2: # Method 3 |
||
| 119 | n, q = rounds // 4, rounds % 4 |
||
| 120 | if q == 1: |
||
| 121 | return 0.25 * data[n - 1] + 0.75 * data[n] |
||
| 122 | else: |
||
| 123 | return 0.75 * data[n] + 0.25 * data[n + 1] |
||
| 124 | else: # Method 2 |
||
| 125 | return statistics.median(data[:rounds // 2]) |
||
| 126 | |||
| 127 | View Code Duplication | @cached_property |
|
| 128 | def q3(self): |
||
| 129 | rounds = self.rounds |
||
| 130 | data = self.sorted_data |
||
| 131 | |||
| 132 | # See: https://en.wikipedia.org/wiki/Quartile#Computing_methods |
||
| 133 | if rounds == 1: |
||
| 134 | return data[0] |
||
| 135 | elif rounds % 2: # Method 3 |
||
| 136 | n, q = rounds // 4, rounds % 4 |
||
| 137 | if q == 1: |
||
| 138 | return 0.75 * data[3 * n] + 0.25 * data[3 * n + 1] |
||
| 139 | else: |
||
| 140 | return 0.25 * data[3 * n + 1] + 0.75 * data[3 * n + 2] |
||
| 141 | else: # Method 2 |
||
| 142 | return statistics.median(data[rounds // 2:]) |
||
| 143 | |||
| 144 | @cached_property |
||
| 145 | def iqr(self): |
||
| 146 | return self.q3 - self.q1 |
||
| 147 | |||
| 148 | @property |
||
| 149 | def iqr_outliers(self): |
||
| 150 | """ |
||
| 151 | Count of Tukey outliers: what's beyond (Q1 - 1.5IQR, Q3 + 1.5IQR) |
||
| 152 | """ |
||
| 153 | count = 0 |
||
| 154 | q0 = self.q1 - 1.5 * self.iqr |
||
| 155 | q4 = self.q3 + 1.5 * self.iqr |
||
| 156 | for val in self.data: |
||
| 157 | if val < q0 or val > q4: |
||
| 158 | count += 1 |
||
| 159 | return count |
||
| 160 | |||
| 161 | @cached_property |
||
| 162 | def outliers(self): |
||
| 163 | return "%s;%s" % (self.stddev_outliers, self.iqr_outliers) |
||
| 164 | |||
| 165 | @cached_property |
||
| 166 | def ops(self): |
||
| 167 | if self.total: |
||
| 168 | return self.rounds / self.total |
||
| 169 | return 0 |
||
| 170 | |||
| 258 |