Total Complexity | 60 |
Total Lines | 179 |
Duplicated Lines | 88.27 % |
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.level.Level 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 | # |
||
2 | # SOFTWARE HISTORY |
||
3 | # |
||
4 | # Date Ticket# Engineer Description |
||
5 | # ------------ ---------- ----------- -------------------------- |
||
6 | # 05/29/13 2023 dgilling Initial Creation. |
||
7 | # 02/12/14 2672 bsteffen Allow String constructor to parse floats. |
||
8 | # 06/29/15 4480 dgilling Implement __hash__, __eq__, |
||
9 | # __str__ and rich comparison operators. |
||
10 | # |
||
11 | |||
12 | import re |
||
13 | import numpy |
||
14 | |||
15 | from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.level import MasterLevel |
||
16 | |||
17 | LEVEL_NAMING_REGEX = re.compile("^(\d*(?:\.\d*)?)(?:_(\d*(?:\.\d*)?))?([a-zA-Z]+)$") |
||
18 | INVALID_VALUE = numpy.float64(-999999) |
||
19 | |||
20 | |||
21 | View Code Duplication | class Level(object): |
|
|
|||
22 | |||
23 | def __init__(self, levelString=None): |
||
24 | self.id = 0 |
||
25 | self.identifier = None |
||
26 | self.masterLevel = None |
||
27 | self.levelonevalue = INVALID_VALUE |
||
28 | self.leveltwovalue = INVALID_VALUE |
||
29 | |||
30 | if levelString is not None: |
||
31 | matcher = LEVEL_NAMING_REGEX.match(str(levelString)) |
||
32 | if matcher is not None: |
||
33 | self.levelonevalue = numpy.float64(matcher.group(1)) |
||
34 | self.masterLevel = MasterLevel.MasterLevel(matcher.group(3)) |
||
35 | levelTwo = matcher.group(2) |
||
36 | if levelTwo: |
||
37 | self.leveltwovalue = numpy.float64(levelTwo) |
||
38 | |||
39 | def __hash__(self): |
||
40 | # XOR-ing the 3 items in a tuple ensures that order of the |
||
41 | # values matters |
||
42 | hashCode = hash(self.masterLevel) ^ hash(self.levelonevalue) ^ hash(self.leveltwovalue) |
||
43 | hashCode ^= hash((self.masterLevel, self.levelonevalue, self.leveltwovalue)) |
||
44 | return hashCode |
||
45 | |||
46 | def __eq__(self, other): |
||
47 | if isinstance(self, type(other)): |
||
48 | return (self.masterLevel, self.levelonevalue, self.leveltwovalue) == \ |
||
49 | (other.masterLevel, other.levelonevalue, other.leveltwovalue) |
||
50 | return False |
||
51 | |||
52 | def __ne__(self, other): |
||
53 | return not self.__eq__(other) |
||
54 | |||
55 | def __lt__(self, other): |
||
56 | if not isinstance(self, type(other)): |
||
57 | return NotImplemented |
||
58 | elif self.masterLevel.getName() != other.masterLevel.getName(): |
||
59 | return NotImplemented |
||
60 | |||
61 | myLevel1 = self.levelonevalue |
||
62 | myLevel2 = self.leveltwovalue |
||
63 | otherLevel1 = other.levelonevalue |
||
64 | otherLevel2 = other.leveltwovalue |
||
65 | if myLevel1 == INVALID_VALUE and myLevel2 != INVALID_VALUE: |
||
66 | myLevel1 = myLevel2 |
||
67 | myLevel2 = INVALID_VALUE |
||
68 | if otherLevel1 == INVALID_VALUE and otherLevel2 != INVALID_VALUE: |
||
69 | otherLevel1 = otherLevel2 |
||
70 | otherLevel2 = INVALID_VALUE |
||
71 | |||
72 | # We default to descending order to make sorting levels from the DAF easier |
||
73 | compareType = self.masterLevel.getType() if self.masterLevel.getType() else "DEC" |
||
74 | if myLevel1 != INVALID_VALUE and otherLevel1 != INVALID_VALUE: |
||
75 | level1Cmp = self.__compareLevelValues(compareType, myLevel1, otherLevel1) |
||
76 | if level1Cmp == -1: |
||
77 | if myLevel2 != INVALID_VALUE and otherLevel2 != INVALID_VALUE: |
||
78 | level2Cmp = self.__compareLevelValues(compareType, myLevel2, otherLevel2) |
||
79 | return level2Cmp == -1 |
||
80 | elif myLevel2 != INVALID_VALUE: |
||
81 | level2Cmp = self.__compareLevelValues(compareType, myLevel2, otherLevel1) |
||
82 | return level2Cmp == -1 |
||
83 | return True |
||
84 | return False |
||
85 | |||
86 | def __le__(self, other): |
||
87 | if not isinstance(self, type(other)): |
||
88 | return NotImplemented |
||
89 | elif self.masterLevel.getName() != other.masterLevel.getName(): |
||
90 | return NotImplemented |
||
91 | |||
92 | return self.__lt__(other) or self.__eq__(other) |
||
93 | |||
94 | def __gt__(self, other): |
||
95 | if not isinstance(self, type(other)): |
||
96 | return NotImplemented |
||
97 | elif self.masterLevel.getName() != other.masterLevel.getName(): |
||
98 | return NotImplemented |
||
99 | |||
100 | myLevel1 = self.levelonevalue |
||
101 | myLevel2 = self.leveltwovalue |
||
102 | otherLevel1 = other.levelonevalue |
||
103 | otherLevel2 = other.leveltwovalue |
||
104 | if myLevel1 == INVALID_VALUE and myLevel2 != INVALID_VALUE: |
||
105 | myLevel1 = myLevel2 |
||
106 | myLevel2 = INVALID_VALUE |
||
107 | if otherLevel1 == INVALID_VALUE and otherLevel2 != INVALID_VALUE: |
||
108 | otherLevel1 = otherLevel2 |
||
109 | otherLevel2 = INVALID_VALUE |
||
110 | |||
111 | # We default to descending order to make sorting levels from the DAF easier |
||
112 | compareType = self.masterLevel.getType() if self.masterLevel.getType() else "DEC" |
||
113 | if myLevel1 != INVALID_VALUE and otherLevel1 != INVALID_VALUE: |
||
114 | level1Cmp = self.__compareLevelValues(compareType, myLevel1, otherLevel1) |
||
115 | if level1Cmp == 1: |
||
116 | if myLevel2 != INVALID_VALUE and otherLevel2 != INVALID_VALUE: |
||
117 | level2Cmp = self.__compareLevelValues(compareType, myLevel2, otherLevel2) |
||
118 | return level2Cmp == 1 |
||
119 | elif otherLevel2 != INVALID_VALUE: |
||
120 | level2Cmp = self.__compareLevelValues(compareType, myLevel1, otherLevel2) |
||
121 | return level2Cmp == 1 |
||
122 | return True |
||
123 | return False |
||
124 | |||
125 | def __ge__(self, other): |
||
126 | if not isinstance(self, type(other)): |
||
127 | return NotImplemented |
||
128 | elif self.masterLevel.getName() != other.masterLevel.getName(): |
||
129 | return NotImplemented |
||
130 | |||
131 | return self.__gt__(other) or self.__eq__(other) |
||
132 | |||
133 | def __compareLevelValues(self, compareType, val1, val2): |
||
134 | returnVal = 0 |
||
135 | if val1 < val2: |
||
136 | returnVal = -1 if compareType == 'INC' else 1 |
||
137 | elif val2 < val1: |
||
138 | returnVal = 1 if compareType == 'INC' else -1 |
||
139 | return returnVal |
||
140 | |||
141 | def __str__(self): |
||
142 | retVal = "" |
||
143 | if INVALID_VALUE != self.levelonevalue: |
||
144 | retVal += str(self.levelonevalue) |
||
145 | if INVALID_VALUE != self.leveltwovalue: |
||
146 | retVal += "_" + str(self.leveltwovalue) |
||
147 | retVal += str(self.masterLevel.getName()) |
||
148 | return retVal |
||
149 | |||
150 | def getId(self): |
||
151 | return self.id |
||
152 | |||
153 | def setId(self, levelid): |
||
154 | self.id = levelid |
||
155 | |||
156 | def getMasterLevel(self): |
||
157 | return self.masterLevel |
||
158 | |||
159 | def setMasterLevel(self, masterLevel): |
||
160 | self.masterLevel = masterLevel |
||
161 | |||
162 | def getLevelonevalue(self): |
||
163 | return self.levelonevalue |
||
164 | |||
165 | def setLevelonevalue(self, levelonevalue): |
||
166 | self.levelonevalue = numpy.float64(levelonevalue) |
||
167 | |||
168 | def getLeveltwovalue(self): |
||
169 | return self.leveltwovalue |
||
170 | |||
171 | def setLeveltwovalue(self, leveltwovalue): |
||
172 | self.leveltwovalue = numpy.float64(leveltwovalue) |
||
173 | |||
174 | def getIdentifier(self): |
||
175 | return self.identifier |
||
176 | |||
177 | def setIdentifier(self, identifier): |
||
178 | self.identifier = identifier |
||
179 |