Conditions | 13 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ed2d.assets.OBJ.__process_in_house() 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 | |||
73 | def __process_in_house(self, filename): |
||
74 | with open(filename, "r") as objfl: |
||
75 | |||
76 | for line in objfl: |
||
77 | |||
78 | value = line.split() |
||
79 | valueType = value[0] |
||
80 | |||
81 | # Don't bother unless the following key words exist in the line |
||
82 | if valueType not in ['f', 'v', 'vt', 'vn', 'g', 'usemtl']: |
||
83 | continue |
||
84 | |||
85 | # Start ignoring the first word of the line to grab the values |
||
86 | value = value[1:] |
||
87 | matname = None |
||
88 | # Check first and continue on early because of string splitting |
||
89 | if valueType == "usemtl": |
||
90 | matname = value[1] |
||
91 | # Material Vertex UV Normal Indices Group (Vertex, UV, Normal) |
||
92 | self.tmvnig[matname] = [[],[],[]] |
||
93 | self.matnumber += 1 |
||
94 | continue |
||
95 | |||
96 | if valueType == "f": |
||
97 | temp = [item.split("/") for item in value] |
||
98 | |||
99 | for i in range(3): |
||
100 | # 0 - Vertex |
||
101 | # 1 - UV |
||
102 | # 2 - Normal |
||
103 | # Make sure UV index data exists |
||
104 | if temp[i][1] != '': |
||
105 | self.tmvnig[matname][1][self.fnumber] = int(temp[i][1]) |
||
106 | self.tmvnig[matname][0][self.fnumber] = int(temp[i][0]) |
||
107 | self.tmvnig[matname][2][self.fnumber] = int(temp[i][2]) |
||
108 | self.fnumber += 1 |
||
109 | continue |
||
110 | |||
111 | # Map the values after the keyword to floats |
||
112 | value = list(map(float, value)) |
||
113 | |||
114 | if valueType == "v": |
||
115 | v = [value[0], value[1], value[2]] |
||
116 | self.tempVertices[self.vnumber] = v |
||
117 | self.vnumber += 1 |
||
118 | |||
119 | elif valueType == "vt": |
||
120 | vt = [value[0], value[0]] |
||
121 | self.tempUVs[self.vtnumber] = vt |
||
122 | self.vtnumber += 1 |
||
123 | |||
124 | elif valueType == "vn": |
||
125 | n = [value[0], value[1], value[2]] |
||
126 | self.tempNormals[self.vnnumber] = n |
||
127 | self.vnnumber += 1 |
||
128 | |||
129 | elif valueType == "g": |
||
130 | g = value[0] |
||
131 | self.tempMaterials[self.matnumber] = g |
||
132 | self.matnumber += 1 |
||
133 | |||
154 |