| Conditions | 12 |
| Total Lines | 72 |
| Code Lines | 46 |
| Lines | 26 |
| Ratio | 36.11 % |
| Changes | 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 bika.lims.exportimport.instruments.shimadzu.gcms.qp2010se.GCMSQP2010SECSVParser.parse_quantitationesultsline() 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 | # -*- coding: utf-8 -*- |
||
| 208 | def parse_quantitationesultsline(self, line): |
||
| 209 | """ Parses quantitation result lines |
||
| 210 | Please see samples/GC-MS output.txt |
||
| 211 | [MS Quantitative Results] section |
||
| 212 | """ |
||
| 213 | |||
| 214 | # [MS Quantitative Results] |
||
| 215 | if line.startswith(self.QUANTITATIONRESULTS_KEY) \ |
||
| 216 | or line.startswith(self.QUANTITATIONRESULTS_NUMBEROFIDS) \ |
||
| 217 | or line.startswith(self.SIMILARITYSEARCHRESULTS_KEY) \ |
||
| 218 | or line.startswith(self.PEAK_TABLE_KEY): |
||
| 219 | |||
| 220 | # Nothing to do, continue |
||
| 221 | return 0 |
||
| 222 | |||
| 223 | # # of IDs \t23 |
||
| 224 | if line.startswith(self.QUANTITATIONRESULTS_HEADER_ID_NUMBER): |
||
| 225 | self._quantitationresultsheader = [token.strip() for token |
||
| 226 | in line.split('\t') |
||
| 227 | if token.strip()] |
||
| 228 | return 0 |
||
| 229 | |||
| 230 | # 1 \talpha-Pinene \tTarget \t0 \t93.00 \t7.738 \t7.680 \t7.795 \t2.480 |
||
| 231 | # \t344488 \t138926 \t0.02604 \tAuto \t2 \t7.812 \tLinear \t0 \t0 |
||
| 232 | # \t4.44061e+008 \t278569 \t0 \t0 \t38.94 \t38.58 \t0.00 \t98 \t92.00 |
||
| 233 | # \t0 \t0 \t38.94 \t38.58 \t91.00 \t0 \t0 \t38.93 \t40.02 \t0 \t0 \t0 |
||
| 234 | # \t0 \t0 \t0 \t0 #\t0 \t0 \t0 \t0 \t0 \t0 \t0 \t0 \t75.27 \tmg \t0.000 |
||
| 235 | splitted = [token.strip() for token in line.split('\t')] |
||
| 236 | ar_id = self._header['Data File Name'].split('\\')[-1].split('.')[0] |
||
| 237 | quantitation = {'DefaultResult': 'Conc.', 'AR': ar_id} |
||
| 238 | for colname in self._quantitationresultsheader: |
||
| 239 | quantitation[colname] = '' |
||
| 240 | |||
| 241 | for i in range(len(splitted)): |
||
| 242 | token = splitted[i] |
||
| 243 | View Code Duplication | if i < len(self._quantitationresultsheader): |
|
| 244 | colname = self._quantitationresultsheader[i] |
||
| 245 | if colname in self.QUANTITATIONRESULTS_NUMERICHEADERS: |
||
| 246 | try: |
||
| 247 | quantitation[colname] = float(token) |
||
| 248 | except ValueError: |
||
| 249 | self.warn( |
||
| 250 | "No valid number ${token} in column " |
||
| 251 | "${index} (${column_name})", |
||
| 252 | mapping={"token": token, |
||
| 253 | "index": str(i + 1), |
||
| 254 | "column_name": colname}, |
||
| 255 | numline=self._numline, line=line) |
||
| 256 | quantitation[colname] = token |
||
| 257 | else: |
||
| 258 | quantitation[colname] = token |
||
| 259 | |||
| 260 | # val = re.sub(r"\W", "", splitted[1]) |
||
| 261 | # self._addRawResult(quantitation['AR'], |
||
| 262 | # values={val:quantitation}, |
||
| 263 | # override=False) |
||
| 264 | elif token: |
||
| 265 | self.err("Orphan value in column ${index} (${token})", |
||
| 266 | mapping={"index": str(i+1), |
||
| 267 | "token": token}, |
||
| 268 | numline=self._numline, line=line) |
||
| 269 | |||
| 270 | result = quantitation[quantitation['DefaultResult']] |
||
| 271 | column_name = quantitation['DefaultResult'] |
||
| 272 | result = self.zeroValueDefaultInstrumentResults(column_name, |
||
| 273 | result, line) |
||
| 274 | quantitation[quantitation['DefaultResult']] = result |
||
| 275 | |||
| 276 | val = re.sub(r"\W", "", splitted[1]) |
||
| 277 | self._addRawResult(quantitation['AR'], |
||
| 278 | values={val: quantitation}, |
||
| 279 | override=False) |
||
| 280 | |||
| 309 |