Conditions | 12 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 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 HDRLoader.load() 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 | import math |
||
119 | def load(self, fileName, hdrf): |
||
120 | f = open(fileName, 'rb') |
||
121 | |||
122 | strf = ''.join(struct.unpack('10s', f.read(10))) |
||
123 | if strf != '#?RADIANCE': |
||
124 | f.close() |
||
125 | return False |
||
126 | |||
127 | f.seek(1, 1) |
||
128 | |||
129 | cmd = [] |
||
130 | c = 0 |
||
131 | oldc = 0 |
||
132 | while True: |
||
133 | oldc = c |
||
134 | c = ord(f.read(1)) |
||
135 | if c is 0xa and oldc is 0xa: |
||
136 | break |
||
137 | cmd.append(str(sm.unichr(c))) |
||
138 | |||
139 | reso = [] |
||
140 | while True: |
||
141 | c = ord(f.read(1)) |
||
142 | cstr = str(sm.unichr(c)) |
||
143 | reso.append(cstr) |
||
144 | if c is 0xa: |
||
145 | break |
||
146 | |||
147 | resoStr = "".join(reso) |
||
148 | resoStrUnformat = re.match('\-Y (?P<_0>\d+) \+X (?P<_1>\d+)', resoStr) |
||
149 | (ws, hs) = map(itemgetter(1), sorted(resoStrUnformat.groupdict().items())) |
||
150 | |||
151 | w = int(ws) |
||
152 | h = int(hs) |
||
153 | |||
154 | hdrf.width = w |
||
155 | hdrf.height = h |
||
156 | |||
157 | self.scanline = [[-1 for i in range(4)] for i in range(w)] |
||
158 | |||
159 | if not self.scanline: |
||
160 | print("File closed because scanline not found.") |
||
161 | f.close() |
||
162 | return False |
||
163 | |||
164 | scnidx = 0 |
||
165 | # Convert image |
||
166 | for [] in range(h - 1, -1, -1): |
||
167 | # If self.scanline doesn't update is because of this |
||
168 | if (self.deCrunch(scnidx, w, f) is False): |
||
169 | break |
||
170 | # This should update the cols array in hdrf which is the HDR Class. |
||
171 | # If hdrf.cols doesn't update is because of this |
||
172 | self.workOnRGBE(hdrf, w) |
||
173 | scnidx = scnidx + 1 |
||
174 | |||
175 | f.close() |
||
176 | |||
190 |