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