| Conditions | 40 |
| Total Lines | 191 |
| Code Lines | 122 |
| Lines | 11 |
| Ratio | 5.76 % |
| 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.agilent.masshunter.masshunter.AgilentMasshunterParser.parse_headerline() 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 -*- |
||
| 112 | def parse_headerline(self, line): |
||
| 113 | """ Parses header lines |
||
| 114 | |||
| 115 | Analysis Time 7/13/2017 9:55 |
||
| 116 | Analyst Name MassHunter01\Agilent |
||
| 117 | Batch Data Path D:\MassHunter\GCMS\Terpenes\2017\July\20170711\ |
||
| 118 | QuantResults\20170711 Sample Workup |
||
| 119 | Batch Name 20170711 Sample Workup |
||
| 120 | Batch State Processed |
||
| 121 | Calibration Last Updated Time 6/29/2017 15:57 |
||
| 122 | Report Generation Time 1/1/0001 12:00:00 AM |
||
| 123 | Report Generator Name None |
||
| 124 | Report Results Data Path None |
||
| 125 | SchemaVersion 65586 |
||
| 126 | Quant Batch Version B.08.00 |
||
| 127 | Quant Report Version B.08.00 |
||
| 128 | """ |
||
| 129 | if self._end_header is True: |
||
| 130 | # Header already processed |
||
| 131 | return 0 |
||
| 132 | |||
| 133 | splitted = [token.strip() for token in line.split(self.COMMAS)] |
||
| 134 | |||
| 135 | # Analysis Time 7/13/2017 9:55 |
||
| 136 | if splitted[0] == self.HEADERKEY_ANALYSISTIME: |
||
| 137 | View Code Duplication | if splitted[1]: |
|
| 138 | try: |
||
| 139 | d = datetime.strptime(splitted[1], "%m/%d/%Y %H:%M") |
||
| 140 | self._header[self.HEADERKEY_ANALYSISTIME] = d |
||
| 141 | except ValueError: |
||
| 142 | self.err("Invalid Output Time format", |
||
| 143 | numline=self._numline, line=line) |
||
| 144 | else: |
||
| 145 | self.warn("Output Time not found or empty", |
||
| 146 | numline=self._numline, line=line) |
||
| 147 | d = datetime.strptime(splitted[1], "%I:%M %p") |
||
| 148 | |||
| 149 | # Analyst Name MassHunter01\Agilent |
||
| 150 | elif splitted[0] == self.HEADERKEY_ANALYSTNAME: |
||
| 151 | if self.HEADERKEY_ANALYSTNAME in self._header: |
||
| 152 | self.warn("Header File Data Name already found. Discarding", |
||
| 153 | numline=self._numline, line=line) |
||
| 154 | return 0 |
||
| 155 | |||
| 156 | if splitted[1]: |
||
| 157 | self._header[self.HEADERKEY_ANALYSTNAME] = splitted[1] |
||
| 158 | else: |
||
| 159 | self.warn("File Data Name not found or empty", |
||
| 160 | numline=self._numline, line=line) |
||
| 161 | |||
| 162 | # Batch Data Path |
||
| 163 | # D:\MassHunter\GCMS\Terpenes\2017\July\20170711\QuantResults\20170711 |
||
| 164 | elif splitted[0] == self.HEADERKEY_BATCHDATAPATH: |
||
| 165 | if self.HEADERKEY_BATCHDATAPATH in self._header: |
||
| 166 | self.warn("Header File Data Name already found. Discarding", |
||
| 167 | numline=self._numline, line=line) |
||
| 168 | return 0 |
||
| 169 | |||
| 170 | if splitted[1]: |
||
| 171 | self._header[self.HEADERKEY_BATCHDATAPATH] = splitted[1] |
||
| 172 | else: |
||
| 173 | self.warn("File Data Name not found or empty", |
||
| 174 | numline=self._numline, line=line) |
||
| 175 | |||
| 176 | # Batch Name 20170711 Sample Workup |
||
| 177 | elif splitted[0] == self.HEADERKEY_BATCHNAME: |
||
| 178 | if self.HEADERKEY_BATCHNAME in self._header: |
||
| 179 | self.warn("Header File Data Name already found. Discarding", |
||
| 180 | numline=self._numline, line=line) |
||
| 181 | return 0 |
||
| 182 | |||
| 183 | if splitted[1]: |
||
| 184 | self._header[self.HEADERKEY_BATCHNAME] = splitted[1] |
||
| 185 | else: |
||
| 186 | self.warn("File Data Name not found or empty", |
||
| 187 | numline=self._numline, line=line) |
||
| 188 | |||
| 189 | # Batch State Processed |
||
| 190 | elif splitted[0] == self.HEADERKEY_BATCHSTATE: |
||
| 191 | if self.HEADERKEY_BATCHSTATE in self._header: |
||
| 192 | self.warn("Header File Data Name already found. Discarding", |
||
| 193 | numline=self._numline, line=line) |
||
| 194 | return 0 |
||
| 195 | |||
| 196 | if splitted[1]: |
||
| 197 | self._header[self.HEADERKEY_BATCHNAME] = splitted[1] |
||
| 198 | else: |
||
| 199 | self.warn("File Data Name not found or empty", |
||
| 200 | numline=self._numline, line=line) |
||
| 201 | |||
| 202 | # Calibration Last Updated Time 6/29/2017 15:57 |
||
| 203 | elif splitted[0] == self.HEADERKEY_LASTCALIBRATION: |
||
| 204 | if self.HEADERKEY_LASTCALIBRATION in self._header: |
||
| 205 | self.warn("Header File Data Name already found. Discarding", |
||
| 206 | numline=self._numline, line=line) |
||
| 207 | return 0 |
||
| 208 | |||
| 209 | if splitted[1]: |
||
| 210 | self._header[self.HEADERKEY_LASTCALIBRATION] = splitted[1] |
||
| 211 | else: |
||
| 212 | self.warn("File Data Name not found or empty", |
||
| 213 | numline=self._numline, line=line) |
||
| 214 | |||
| 215 | # Report Generation Time 1/1/0001 12:00:00 AM |
||
| 216 | elif splitted[0] == self.HEADERKEY_REPORTGENERATIONTIME: |
||
| 217 | if self.HEADERKEY_REPORTGENERATIONTIME in self._header: |
||
| 218 | self.warn("Header File Data Name already found. Discarding", |
||
| 219 | numline=self._numline, line=line) |
||
| 220 | return 0 |
||
| 221 | |||
| 222 | if splitted[1]: |
||
| 223 | self._header[self.HEADERKEY_REPORTGENERATIONTIME] = splitted[1] |
||
| 224 | else: |
||
| 225 | self.warn("File Data Name not found or empty", |
||
| 226 | numline=self._numline, line=line) |
||
| 227 | |||
| 228 | # Report Generator Name None |
||
| 229 | elif splitted[0] == self.HEADERKEY_REPORTGENERATORNAME: |
||
| 230 | if self.HEADERKEY_REPORTGENERATORNAME in self._header: |
||
| 231 | self.warn("Header File Data Name already found. Discarding", |
||
| 232 | numline=self._numline, line=line) |
||
| 233 | return 0 |
||
| 234 | |||
| 235 | if splitted[1]: |
||
| 236 | self._header[self.HEADERKEY_REPORTGENERATORNAME] = splitted[1] |
||
| 237 | else: |
||
| 238 | self.warn("File Data Name not found or empty", |
||
| 239 | numline=self._numline, line=line) |
||
| 240 | |||
| 241 | # Report Results Data Path None |
||
| 242 | elif splitted[0] == self.HEADERKEY_REPORTRESULTSDATAPATH: |
||
| 243 | if self.HEADERKEY_REPORTRESULTSDATAPATH in self._header: |
||
| 244 | self.warn("Header File Data Name already found. Discarding", |
||
| 245 | numline=self._numline, line=line) |
||
| 246 | return 0 |
||
| 247 | |||
| 248 | if splitted[1]: |
||
| 249 | self._header[self.HEADERKEY_REPORTRESULTSDATAPATH] = \ |
||
| 250 | splitted[1] |
||
| 251 | else: |
||
| 252 | self.warn("File Data Name not found or empty", |
||
| 253 | numline=self._numline, line=line) |
||
| 254 | |||
| 255 | # SchemaVersion 65586 |
||
| 256 | elif splitted[0] == self.HEADERKEY_SCHEMAVERSION: |
||
| 257 | if self.HEADERKEY_SCHEMAVERSION in self._header: |
||
| 258 | self.warn("Header File Data Name already found. Discarding", |
||
| 259 | numline=self._numline, line=line) |
||
| 260 | return 0 |
||
| 261 | |||
| 262 | if splitted[1]: |
||
| 263 | self._header[self.HEADERKEY_SCHEMAVERSION] = splitted[1] |
||
| 264 | else: |
||
| 265 | self.warn("File Data Name not found or empty", |
||
| 266 | numline=self._numline, line=line) |
||
| 267 | |||
| 268 | # Quant Batch Version B.08.00 |
||
| 269 | elif splitted[0] == self.HEADERKEY_QUANTBATCHVERSION: |
||
| 270 | if self.HEADERKEY_QUANTBATCHVERSION in self._header: |
||
| 271 | self.warn("Header File Data Name already found. Discarding", |
||
| 272 | numline=self._numline, line=line) |
||
| 273 | return 0 |
||
| 274 | |||
| 275 | if splitted[1]: |
||
| 276 | self._header[self.HEADERKEY_QUANTBATCHVERSION] = splitted[1] |
||
| 277 | else: |
||
| 278 | self.warn("File Data Name not found or empty", |
||
| 279 | numline=self._numline, line=line) |
||
| 280 | |||
| 281 | # Quant Report Version B.08.00 |
||
| 282 | elif splitted[0] == self.HEADERKEY_QUANTREPORTVERSION: |
||
| 283 | if self.HEADERKEY_QUANTREPORTVERSION in self._header: |
||
| 284 | self.warn("Header File Data Name already found. Discarding", |
||
| 285 | numline=self._numline, line=line) |
||
| 286 | return 0 |
||
| 287 | |||
| 288 | if splitted[1]: |
||
| 289 | self._header[self.HEADERKEY_QUANTREPORTVERSION] = splitted[1] |
||
| 290 | else: |
||
| 291 | self.warn("File Data Name not found or empty", |
||
| 292 | numline=self._numline, line=line) |
||
| 293 | |||
| 294 | # Blank lines |
||
| 295 | if splitted[0] == '': |
||
| 296 | self._end_header = True |
||
| 297 | if len(self._header) == 0: |
||
| 298 | self.err("No header found", numline=self._numline) |
||
| 299 | return -1 |
||
| 300 | return 0 |
||
| 301 | |||
| 302 | return 0 |
||
| 303 | |||
| 391 |