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 |
||
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 | self.platform_name = h5f.attrs['platform_name'] |
||
133 | if not self.instrument: |
||
134 | try: |
||
135 | self.instrument = h5f.attrs['sensor'] |
||
136 | except KeyError: |
||
137 | LOG.warning("No sensor name specified in HDF5 file") |
||
138 | self.instrument = INSTRUMENTS.get(self.platform_name) |
||
139 | |||
140 | for bandname in self.band_names: |
||
141 | self.rsr[bandname] = {} |
||
142 | try: |
||
143 | num_of_det = h5f[bandname].attrs['number_of_detectors'] |
||
144 | except KeyError: |
||
145 | if not no_detectors_message: |
||
146 | LOG.debug("No detectors found - assume only one...") |
||
147 | num_of_det = 1 |
||
148 | no_detectors_message = True |
||
149 | |||
150 | for i in range(1, num_of_det + 1): |
||
151 | dname = 'det-{0:d}'.format(i) |
||
152 | self.rsr[bandname][dname] = {} |
||
153 | try: |
||
154 | resp = h5f[bandname][dname]['response'][:] |
||
155 | except KeyError: |
||
156 | resp = h5f[bandname]['response'][:] |
||
157 | |||
158 | self.rsr[bandname][dname]['response'] = resp |
||
159 | |||
160 | try: |
||
161 | wvl = (h5f[bandname][dname]['wavelength'][:] * |
||
162 | h5f[bandname][dname][ |
||
163 | 'wavelength'].attrs['scale']) |
||
164 | except KeyError: |
||
165 | wvl = (h5f[bandname]['wavelength'][:] * |
||
166 | h5f[bandname]['wavelength'].attrs['scale']) |
||
167 | |||
168 | # The wavelength is given in micro meters! |
||
169 | self.rsr[bandname][dname]['wavelength'] = wvl * 1e6 |
||
170 | |||
171 | try: |
||
172 | central_wvl = h5f[bandname][ |
||
173 | dname].attrs['central_wavelength'] |
||
174 | except KeyError: |
||
175 | central_wvl = h5f[bandname].attrs['central_wavelength'] |
||
176 | |||
177 | self.rsr[bandname][dname][ |
||
178 | 'central_wavelength'] = central_wvl |
||
179 | |||
218 |