Conditions | 14 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 RelativeSpectralResponse.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 | #!/usr/bin/env python |
||
123 | def load(self): |
||
124 | """Read the internally formatet hdf5 relative spectral response data""" |
||
125 | import h5py |
||
126 | |||
127 | no_detectors_message = False |
||
128 | with h5py.File(self.filename, 'r') as h5f: |
||
129 | self.band_names = h5f.attrs['band_names'].tolist() |
||
130 | self.description = h5f.attrs['description'] |
||
131 | if not self.platform_name: |
||
132 | try: |
||
133 | self.platform_name = h5f.attrs['platform_name'] |
||
134 | except KeyError: |
||
135 | LOG.warning("No platform_name in HDF5 file") |
||
136 | try: |
||
137 | self.platform_name = h5f.attrs[ |
||
138 | 'platform'] + '-' + h5f.attrs['satnum'] |
||
139 | except KeyError: |
||
140 | LOG.warning( |
||
141 | "Unable to determine platform name from HDF5 file content") |
||
142 | self.platform_name = None |
||
143 | |||
144 | if not self.instrument: |
||
145 | try: |
||
146 | self.instrument = h5f.attrs['sensor'] |
||
147 | except KeyError: |
||
148 | LOG.warning("No sensor name specified in HDF5 file") |
||
149 | self.instrument = INSTRUMENTS.get(self.platform_name) |
||
150 | |||
151 | for bandname in self.band_names: |
||
152 | self.rsr[bandname] = {} |
||
153 | try: |
||
154 | num_of_det = h5f[bandname].attrs['number_of_detectors'] |
||
155 | except KeyError: |
||
156 | if not no_detectors_message: |
||
157 | LOG.debug("No detectors found - assume only one...") |
||
158 | num_of_det = 1 |
||
159 | no_detectors_message = True |
||
160 | |||
161 | for i in range(1, num_of_det + 1): |
||
162 | dname = 'det-{0:d}'.format(i) |
||
163 | self.rsr[bandname][dname] = {} |
||
164 | try: |
||
165 | resp = h5f[bandname][dname]['response'][:] |
||
166 | except KeyError: |
||
167 | resp = h5f[bandname]['response'][:] |
||
168 | |||
169 | self.rsr[bandname][dname]['response'] = resp |
||
170 | |||
171 | try: |
||
172 | wvl = (h5f[bandname][dname]['wavelength'][:] * |
||
173 | h5f[bandname][dname][ |
||
174 | 'wavelength'].attrs['scale']) |
||
175 | except KeyError: |
||
176 | wvl = (h5f[bandname]['wavelength'][:] * |
||
177 | h5f[bandname]['wavelength'].attrs['scale']) |
||
178 | |||
179 | # The wavelength is given in micro meters! |
||
180 | self.rsr[bandname][dname]['wavelength'] = wvl * 1e6 |
||
181 | |||
182 | try: |
||
183 | central_wvl = h5f[bandname][ |
||
184 | dname].attrs['central_wavelength'] |
||
185 | except KeyError: |
||
186 | central_wvl = h5f[bandname].attrs['central_wavelength'] |
||
187 | |||
188 | self.rsr[bandname][dname][ |
||
189 | 'central_wavelength'] = central_wvl |
||
190 | |||
229 |