| Total Complexity | 52 |
| Total Lines | 184 |
| Duplicated Lines | 96.74 % |
| Changes | 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 dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects.GridParmInfo 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 dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects import GridLocation |
||
| 2 | from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects import ParmID |
||
| 3 | from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects import TimeConstraints |
||
| 4 | |||
| 5 | |||
| 6 | View Code Duplication | class GridParmInfo(object): |
|
|
|
|||
| 7 | |||
| 8 | def __init__(self, parmid=None, gridLoc=None, gridType="NONE", unit=None, |
||
| 9 | descriptiveName="", minValue=0.0, maxValue=0.0, precision=0, |
||
| 10 | timeIndependentParm=False, timeConstraints=None, rateParm=False): |
||
| 11 | self.parmID = parmid |
||
| 12 | self.gridLoc = gridLoc |
||
| 13 | self.gridType = gridType |
||
| 14 | self.descriptiveName = descriptiveName |
||
| 15 | self.unitString = unit |
||
| 16 | self.minValue = float(minValue) |
||
| 17 | self.maxValue = float(maxValue) |
||
| 18 | self.precision = int(precision) |
||
| 19 | self.rateParm = rateParm |
||
| 20 | self.timeConstraints = timeConstraints |
||
| 21 | self.timeIndependentParm = timeIndependentParm |
||
| 22 | |||
| 23 | # (valid, errors) = self.__validCheck() |
||
| 24 | # if not valid: |
||
| 25 | # errorMessage = "GridParmInfo is invalid: " + str(errors) |
||
| 26 | # warnings.warn(errorMessage) |
||
| 27 | # self.__setDefaultValues() |
||
| 28 | |||
| 29 | def __str__(self): |
||
| 30 | return self.__repr__() |
||
| 31 | |||
| 32 | def __repr__(self): |
||
| 33 | if self.isValid(): |
||
| 34 | out = "ParmID: " + str(self.parmID) + \ |
||
| 35 | " TimeConstraints: " + str(self.timeConstraints) + \ |
||
| 36 | " GridLoc: " + str(self.gridLoc) + \ |
||
| 37 | " Units: " + self.unitString + \ |
||
| 38 | " Name: " + self.descriptiveName + \ |
||
| 39 | " Min/Max AllowedValues: " + str(self.minValue) + "," + \ |
||
| 40 | str(self.maxValue) + " Precision: " + str(self.precision) + \ |
||
| 41 | " TimeIndependent: " + str(self.timeIndependentParm) + \ |
||
| 42 | " RateParm: " + str(self.rateParm) + \ |
||
| 43 | " GridType: " + self.gridType |
||
| 44 | return out |
||
| 45 | return "<Invalid>" |
||
| 46 | |||
| 47 | def __eq__(self, other): |
||
| 48 | if not isinstance(other, GridParmInfo): |
||
| 49 | return False |
||
| 50 | if self.descriptiveName != other.descriptiveName: |
||
| 51 | return False |
||
| 52 | if self.gridLoc != other.gridLoc: |
||
| 53 | return False |
||
| 54 | if self.gridType != other.gridType: |
||
| 55 | return False |
||
| 56 | if self.minValue != other.minValue: |
||
| 57 | return False |
||
| 58 | if self.maxValue != other.maxValue: |
||
| 59 | return False |
||
| 60 | if self.parmID != other.parmID: |
||
| 61 | return False |
||
| 62 | if self.precision != other.precision: |
||
| 63 | return False |
||
| 64 | if self.rateParm != other.rateParm: |
||
| 65 | return False |
||
| 66 | if self.timeConstraints != other.timeConstraints: |
||
| 67 | return False |
||
| 68 | if self.timeIndependentParm != other.timeIndependentParm: |
||
| 69 | return False |
||
| 70 | if self.unitString != other.unitString: |
||
| 71 | return False |
||
| 72 | return True |
||
| 73 | |||
| 74 | def __ne__(self, other): |
||
| 75 | return not self.__eq__(other) |
||
| 76 | |||
| 77 | def __validCheck(self): |
||
| 78 | status = [] |
||
| 79 | |||
| 80 | if not self.parmID.isValid(): |
||
| 81 | status.append("GridParmInfo.ParmID is not valid [" + str(self.parmID) + "]") |
||
| 82 | if not self.timeConstraints.isValid(): |
||
| 83 | status.append("GridParmInfo.TimeConstraints are not valid [" + |
||
| 84 | str(self.timeConstraints) + "]") |
||
| 85 | if not self.gridLoc.isValid(): |
||
| 86 | status.append("GridParmInfo.GridLocation is not valid") |
||
| 87 | if self.timeIndependentParm and self.timeConstraints.anyConstraints(): |
||
| 88 | status.append("GridParmInfo is invalid. There are time constraints" + |
||
| 89 | " for a time independent parm. Constraints: " + |
||
| 90 | str(self.timeConstraints)) |
||
| 91 | if not self.unitString: |
||
| 92 | status.append("GridParmInfo.Units are not defined.") |
||
| 93 | if self.precision < -2 or self.precision > 5: |
||
| 94 | status.append("GridParmInfo is invalid. Precision out of limits." + |
||
| 95 | " Precision is: " + str(self.precision) + ". Must be between -2 and 5.") |
||
| 96 | |||
| 97 | retVal = True |
||
| 98 | if status: |
||
| 99 | retVal = False |
||
| 100 | return retVal, status |
||
| 101 | |||
| 102 | def isValid(self): |
||
| 103 | (valid, errors) = self.__validCheck() |
||
| 104 | return valid |
||
| 105 | |||
| 106 | def __setDefaultValues(self): |
||
| 107 | self.parmID = ParmID() |
||
| 108 | self.gridLoc = GridLocation() |
||
| 109 | self.gridType = "NONE" |
||
| 110 | self.descriptiveName = "" |
||
| 111 | self.unitString = "" |
||
| 112 | self.minValue = 0.0 |
||
| 113 | self.maxValue = 0.0 |
||
| 114 | self.precision = 0 |
||
| 115 | self.rateParm = False |
||
| 116 | self.timeConstraints = TimeConstraints() |
||
| 117 | self.timeIndependentParm = False |
||
| 118 | |||
| 119 | def getParmID(self): |
||
| 120 | return self.parmID |
||
| 121 | |||
| 122 | def setParmID(self, parmID): |
||
| 123 | self.parmID = parmID |
||
| 124 | |||
| 125 | def getGridLoc(self): |
||
| 126 | return self.gridLoc |
||
| 127 | |||
| 128 | def setGridLoc(self, gridLoc): |
||
| 129 | self.gridLoc = gridLoc |
||
| 130 | |||
| 131 | def getGridType(self): |
||
| 132 | return self.gridType |
||
| 133 | |||
| 134 | def setGridType(self, gridType): |
||
| 135 | self.gridType = gridType |
||
| 136 | |||
| 137 | def getDescriptiveName(self): |
||
| 138 | return self.descriptiveName |
||
| 139 | |||
| 140 | def setDescriptiveName(self, descriptiveName): |
||
| 141 | self.descriptiveName = descriptiveName |
||
| 142 | |||
| 143 | def getUnitString(self): |
||
| 144 | return self.unitString |
||
| 145 | |||
| 146 | def setUnitString(self, unitString): |
||
| 147 | self.unitString = unitString |
||
| 148 | |||
| 149 | def getMinValue(self): |
||
| 150 | return self.minValue |
||
| 151 | |||
| 152 | def setMinValue(self, minValue): |
||
| 153 | self.minValue = minValue |
||
| 154 | |||
| 155 | def getMaxValue(self): |
||
| 156 | return self.maxValue |
||
| 157 | |||
| 158 | def setMaxValue(self, maxValue): |
||
| 159 | self.maxValue = maxValue |
||
| 160 | |||
| 161 | def getPrecision(self): |
||
| 162 | return self.precision |
||
| 163 | |||
| 164 | def setPrecision(self, precision): |
||
| 165 | self.precision = precision |
||
| 166 | |||
| 167 | def getRateParm(self): |
||
| 168 | return self.rateParm |
||
| 169 | |||
| 170 | def setRateParm(self, rateParm): |
||
| 171 | self.rateParm = rateParm |
||
| 172 | |||
| 173 | def getTimeConstraints(self): |
||
| 174 | return self.timeConstraints |
||
| 175 | |||
| 176 | def setTimeConstraints(self, timeConstraints): |
||
| 177 | self.timeConstraints = timeConstraints |
||
| 178 | |||
| 179 | def getTimeIndependentParm(self): |
||
| 180 | return self.timeIndependentParm |
||
| 181 | |||
| 182 | def setTimeIndependentParm(self, timeIndependentParm): |
||
| 183 | self.timeIndependentParm = timeIndependentParm |
||
| 184 |