dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects.DatabaseID   C
last analyzed

Complexity

Total Complexity 53

Size/Duplication

Total Lines 195
Duplicated Lines 96.92 %

Importance

Changes 0
Metric Value
wmc 53
eloc 142
dl 189
loc 195
rs 6.96
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A DatabaseID.getFormat() 2 2 1
A DatabaseID.getDbType() 2 2 1
A DatabaseID.setSiteId() 2 2 1
A DatabaseID.getModelName() 2 2 1
A DatabaseID.getSiteId() 2 2 1
A DatabaseID.__init__() 19 19 3
A DatabaseID.setFormat() 2 2 1
A DatabaseID.setDbType() 2 2 1
A DatabaseID.setModelName() 2 2 1
A DatabaseID.isValid() 2 2 1
A DatabaseID.setModelId() 2 2 1
A DatabaseID.getModelId() 2 2 1
A DatabaseID.setShortModelId() 2 2 1
A DatabaseID.getModelTime() 2 2 1
A DatabaseID.setModelTime() 2 2 1
A DatabaseID.getShortModelId() 2 2 1
B DatabaseID.__hash__() 9 9 6
A DatabaseID.__decodeDtg() 7 7 2
A DatabaseID.decodeDtg() 8 8 2
A DatabaseID.__repr__() 2 2 1
B DatabaseID.__decodeIdentifier() 33 33 7
A DatabaseID.__str__() 2 2 1
A DatabaseID.__ne__() 2 2 1
B DatabaseID.__cmp__() 21 21 6
A DatabaseID.__eq__() 4 4 2
A DatabaseID.getModelTimeAsDate() 5 5 2
A DatabaseID.__encodeIdentifier() 19 19 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects.DatabaseID 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
# Modified by njensen to add __repr__
2
3
import time
4
5
6 View Code Duplication
class DatabaseID(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
8
    def __init__(self, dbIdentifier=None):
9
        self.siteId = None
10
        self.format = "NONE"
11
        self.dbType = None
12
        self.modelName = None
13
        self.modelTime = None
14
        self.modelId = None
15
        self.shortModelId = None
16
        if dbIdentifier is not None:
17
            if self.__decodeIdentifier(dbIdentifier):
18
                self.__encodeIdentifier()
19
            else:
20
                self.format = "NONE"
21
                self.dbType = ""
22
                self.siteId = ""
23
                self.modelName = ""
24
                self.modelTime = "00000000_0000"
25
                self.modelId = ""
26
                self.shortModelId = ""
27
28
    def isValid(self):
29
        return self.format != "NONE"
30
31
    def getSiteId(self):
32
        return self.siteId
33
34
    def setSiteId(self, siteId):
35
        self.siteId = siteId
36
37
    def getFormat(self):
38
        return self.format
39
40
    def setFormat(self, dbformat):
41
        self.format = dbformat
42
43
    def getDbType(self):
44
        return self.dbType
45
46
    def setDbType(self, dbType):
47
        self.dbType = dbType
48
49
    def getModelName(self):
50
        return self.modelName
51
52
    def setModelName(self, modelName):
53
        self.modelName = modelName
54
55
    def getModelTime(self):
56
        return self.modelTime
57
58
    def setModelTime(self, modelTime):
59
        self.modelTime = modelTime
60
61
    def getModelId(self):
62
        return self.modelId
63
64
    def setModelId(self, modelId):
65
        self.modelId = modelId
66
67
    def getShortModelId(self):
68
        return self.shortModelId
69
70
    def setShortModelId(self, shortModelId):
71
        self.shortModelId = shortModelId
72
73
    def __encodeIdentifier(self):
74
        if self.dbType is not None:
75
            self.modelId = self.siteId + "_" + self.format + "_" \
76
                           + self.dbType + "_" + self.modelName
77
        else:
78
            self.modelId = self.siteId + "_" + self.format + "__" \
79
                           + self.modelName
80
81
        self.shortModelId = self.modelName
82
        if self.dbType is not None and self.dbType != "":
83
            self.shortModelId += "_" + self.dbType
84
85
        if self.modelTime != "00000000_0000":
86
            self.modelId += "_" + self.modelTime
87
            self.shortModelId += "_" + self.modelTime[6:8] + self.modelTime[9:11]
88
        else:
89
            self.modelId += "_" + "00000000_0000"
90
91
        self.shortModelId += " (" + self.siteId + ")"
92
93
    def __decodeIdentifier(self, dbIdentifier):
94
        self.format = "NONE"
95
        self.dbType = ""
96
        self.siteId = ""
97
        self.modelName = ""
98
        self.modelTime = "00000000_0000"
99
100
        # parse into '_' separated strings
101
        strings = dbIdentifier.split("_")
102
        if len(strings) != 6:
103
            return False
104
105
        # store the data
106
        if strings[1] == "GRID":
107
            self.format = "GRID"
108
        else:
109
            return False
110
111
        self.siteId = strings[0]
112
        self.dbType = strings[2]
113
        self.modelName = strings[3]
114
115
        # date-time group
116
        if len(strings[4]) != 8 or len(strings[5]) != 4:
117
            return False
118
119
        # make sure the digits are there
120
        dtg = strings[4] + '_' + strings[5]  # back together
121
        if dtg != "00000000_0000":
122
            if not self.__decodeDtg(dtg):
123
                return False
124
125
        return True
126
127
    @staticmethod
128
    def decodeDtg(dtgString):
129
        dateStruct = time.gmtime(0)
130
        try:
131
            dateStruct = time.strptime(dtgString, "%Y%m%d_%H%M")
132
        except ValueError:
133
            return False, dateStruct
134
        return True, dateStruct
135
136
    def __decodeDtg(self, dtgString):
137
        try:
138
            time.strptime(dtgString, "%Y%m%d_%H%M")
139
            self.modelTime = dtgString
140
        except ValueError:
141
            return False
142
        return True
143
144
    def getModelTimeAsDate(self):
145
        if self.modelTime == "00000000_0000":
146
            return time.gmtime(0)
147
        else:
148
            return time.strptime(self.modelTime, "%Y%m%d_%H%M")
149
150
    def __str__(self):
151
        return self.__repr__()
152
153
    def __repr__(self):
154
        return self.modelId
155
156
    def __hash__(self):
157
        prime = 31
158
        result = 1
159
        result = prime * result + (0 if self.dbType is None else hash(self.dbType))
160
        result = prime * result + (0 if self.format is None else hash(self.format))
161
        result = prime * result + (0 if self.modelId is None else hash(self.modelId))
162
        result = prime * result + (0 if self.modelTime is None else hash(self.modelTime))
163
        result = prime * result + (0 if self.siteId is None else hash(self.siteId))
164
        return result
165
166
    def __cmp__(self, other):
167
        if not isinstance(other, DatabaseID):
168
            siteComp = cmp(self.siteId, other.siteId)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cmp does not seem to be defined.
Loading history...
169
            if siteComp != 0:
170
                return siteComp
171
172
            formatComp = cmp(self.format, other.format)
173
            if formatComp != 0:
174
                return formatComp
175
176
            typeComp = cmp(self.dbType, other.dbType)
177
            if typeComp != 0:
178
                return typeComp
179
180
            nameComp = cmp(self.modelName, other.modelName)
181
            if nameComp != 0:
182
                return nameComp
183
184
            return -cmp(self.getModelTimeAsDate(), other.getModelTimeAsDate())
185
        else:
186
            return NotImplemented
187
188
    def __eq__(self, other):
189
        if not isinstance(other, DatabaseID):
190
            return False
191
        return str(self) == str(other)
192
193
    def __ne__(self, other):
194
        return not self.__eq__(other)
195