dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects.ParmID   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 132
Duplicated Lines 95.45 %

Importance

Changes 0
Metric Value
wmc 41
eloc 97
dl 126
loc 132
rs 9.1199
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A ParmID.parmNameAndLevel() 7 7 2
A ParmID.__decodeIdentifier() 10 10 2
A ParmID.__ne__() 2 2 1
A ParmID.getParmLevel() 2 2 1
A ParmID.getParmId() 2 2 1
A ParmID.__eq__() 10 10 5
A ParmID.getCompositeName() 2 2 1
A ParmID.getParmName() 2 2 1
A ParmID.__encodeIdentifier() 4 4 1
A ParmID.__repr__() 2 2 1
C ParmID.isValid() 12 12 9
B ParmID.__init__() 28 28 7
A ParmID.__cmp__() 13 13 4
A ParmID.getDbId() 2 2 1
A ParmID.__str__() 2 2 1
A ParmID.__hash__() 2 2 1
A ParmID.getShortParmId() 2 2 1
A ParmID.defaultLevel() 3 3 1

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.ParmID 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
from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects import DatabaseID
4
5
6 View Code Duplication
class ParmID(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
8
    def __init__(self, parmIdentifier=None, dbId=None, level=None):
9
        self.parmName = None
10
        self.parmLevel = None
11
        self.dbId = None
12
        self.compositeName = None
13
        self.shortParmId = None
14
        self.parmId = None
15
16
        if (parmIdentifier is not None) and (dbId is not None):
17
            self.parmName = parmIdentifier
18
19
            if isinstance(dbId, DatabaseID):
20
                self.dbId = dbId
21
            elif isinstance(dbId, str):
22
                self.dbId = DatabaseID(dbId)
23
            else:
24
                raise TypeError("Invalid database ID specified.")
25
26
            if level is None:
27
                self.parmLevel = self.defaultLevel()
28
            else:
29
                self.parmLevel = level
30
31
            self.__encodeIdentifier()
32
33
        elif parmIdentifier is not None:
34
            self.__decodeIdentifier(parmIdentifier)
35
            self.__encodeIdentifier()
36
37
    def getParmName(self):
38
        return self.parmName
39
40
    def getParmLevel(self):
41
        return self.parmLevel
42
43
    def getDbId(self):
44
        return self.dbId
45
46
    def getCompositeName(self):
47
        return self.compositeName
48
49
    def getShortParmId(self):
50
        return self.shortParmId
51
52
    def getParmId(self):
53
        return self.parmId
54
55
    def __decodeIdentifier(self, parmIdentifier):
56
        parts = parmIdentifier.split(":")
57
        nameLevel = parts[0].split("_")
58
        self.dbId = DatabaseID(parts[1])
59
        if len(nameLevel) == 2:
60
            self.parmName = nameLevel[0]
61
            self.parmLevel = nameLevel[1]
62
        else:
63
            self.parmName = nameLevel[0]
64
            self.parmLevel = self.defaultLevel()
65
66
    def __encodeIdentifier(self):
67
        self.compositeName = self.parmName + "_" + self.parmLevel
68
        self.shortParmId = self.compositeName + ":" + self.dbId.getShortModelId()
69
        self.parmId = self.compositeName + ":" + self.dbId.getModelId()
70
71
    def isValid(self):
72
        if len(self.parmName) is None or len(self.parmLevel) is None or self.dbId is None:
73
            return False
74
        if len(self.parmName) < 1 or len(self.parmLevel) < 1 or not self.dbId.isValid():
75
            return False
76
77
        if not self.parmName.isalnum():
78
            return False
79
        if not self.parmLevel.isalnum():
80
            return False
81
82
        return True
83
84
    @staticmethod
85
    def defaultLevel():
86
        return "SFC"
87
88
    @staticmethod
89
    def parmNameAndLevel(composite):
90
        pos = composite.find('_')
91
        if pos != -1:
92
            return composite[:pos], composite[pos+1:]
93
        else:
94
            return composite, "SFC"
95
96
    def __str__(self):
97
        return self.__repr__()
98
99
    def __repr__(self):
100
        return self.parmName + '_' + self.parmLevel + ":" + str(self.dbId)
101
102
    def __hash__(self):
103
        return hash(self.parmId)
104
105
    def __cmp__(self, other):
106
        if isinstance(other, ParmID):
107
            nameComp = cmp(self.parmName, other.parmName)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cmp does not seem to be defined.
Loading history...
108
            if nameComp != 0:
109
                return nameComp
110
111
            levelComp = cmp(self.parmLevel, other.parmLevel)
112
            if levelComp != 0:
113
                return levelComp
114
115
            return cmp(self.dbId, other.dbId)
116
        else:
117
            return NotImplemented
118
119
    def __eq__(self, other):
120
        if not isinstance(other, ParmID):
121
            return False
122
        if self.dbId != other.dbId:
123
            return False
124
        if self.parmLevel != other.parmLevel:
125
            return False
126
        if self.parmName != other.parmName:
127
            return False
128
        return True
129
130
    def __ne__(self, other):
131
        return not self.__eq__(other)
132