| Conditions | 44 |
| Total Lines | 216 |
| Code Lines | 135 |
| Lines | 23 |
| Ratio | 10.65 % |
| 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 senaite.core.exportimport.instruments.panalytical.omnia.AxiosXrfCSVParser.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 -*- |
||
| 206 | def parse_headerline(self, line): |
||
| 207 | #Process incoming header line |
||
| 208 | r""" |
||
| 209 | 29/11/2013 10:15:44 |
||
| 210 | PANalytical |
||
| 211 | "Quantification of sample ESFERA CINZA - 1g H3BO3 - 1:0,5 - NO PPC", |
||
| 212 | |||
| 213 | R.M.S.:,"0,035" |
||
| 214 | Result status:, |
||
| 215 | Sum before normalization:,"119,5 %" |
||
| 216 | Normalised to:,"100,0 %" |
||
| 217 | Sample type:,Pressed powder |
||
| 218 | Initial sample weight (g):,"2,000" |
||
| 219 | Weight after pressing (g):,"3,000" |
||
| 220 | Correction applied for medium:,No |
||
| 221 | Correction applied for film:,No |
||
| 222 | Used Compound list:,Oxides |
||
| 223 | Results database:,omnian 2013 |
||
| 224 | Results database in:,c:\panalytical\superq\userdata |
||
| 225 | """ |
||
| 226 | |||
| 227 | if line.startswith('"Quantification of sample') or line.startswith('Quantification of sample'): |
||
| 228 | line = to_unicode(line) |
||
| 229 | if len(self._header) == 0: |
||
| 230 | self.warn('Unexpected header format', numline=self._numline) |
||
| 231 | return -1 |
||
| 232 | # Remove non important string and double comas to obtein |
||
| 233 | # the sample name free |
||
| 234 | line = line.replace("Quantification of sample ", "") |
||
| 235 | line = line.replace('"', "") |
||
| 236 | splitted = line.split(' - ') |
||
| 237 | |||
| 238 | if len(splitted) > 3:# Maybe we don't need this, i could be all the sample's identifier... |
||
| 239 | self._header['Sample'] = splitted[0].strip(' ') |
||
| 240 | self._header['Quantity'] = splitted[1] |
||
| 241 | self._header['????'] = splitted[2]# At present we |
||
| 242 | # don't know what |
||
| 243 | # is that |
||
| 244 | self._header['PPC'] = splitted[3] |
||
| 245 | |||
| 246 | elif len(splitted) == 1: |
||
| 247 | self._header['Sample'] = splitted[0].replace('Quantification of sample','').strip(' ') |
||
| 248 | |||
| 249 | else: |
||
| 250 | self.warn('Unexpected header format', numline=self._numline) |
||
| 251 | return 1 |
||
| 252 | # Save each header field (that we know) and its own value in the dict |
||
| 253 | if line.startswith('R.M.S.'): |
||
| 254 | |||
| 255 | if len(self._header) == 0: |
||
| 256 | self.err("No header found", numline=self._numline) |
||
| 257 | return -1 |
||
| 258 | |||
| 259 | splitted = self.splitLine(line) |
||
| 260 | if len(splitted) > 1: |
||
| 261 | self._header['R.M.S.'] = splitted[1].replace('"', '').strip() |
||
| 262 | else: |
||
| 263 | self.warn('Unexpected header format', numline=self._numline) |
||
| 264 | return 0 |
||
| 265 | |||
| 266 | View Code Duplication | if line.startswith('Result status'): |
|
| 267 | if len(self._header) == 0: |
||
| 268 | self.err("No header found", numline=self._numline) |
||
| 269 | |||
| 270 | splitted = self.splitLine(line) |
||
| 271 | if len(splitted) > 1: |
||
| 272 | self._header['Result status'] = splitted[1].replace('"', '').strip() |
||
| 273 | else: |
||
| 274 | self.warn('Unexpected header format', numline=self._numline) |
||
| 275 | |||
| 276 | return 0 |
||
| 277 | |||
| 278 | if line.startswith('Sum before normalization'): |
||
| 279 | if len(self._header) == 0: |
||
| 280 | self.err("No header found", numline=self._numline) |
||
| 281 | return -1 |
||
| 282 | |||
| 283 | splitted = self.splitLine(line) |
||
| 284 | if len(splitted) > 1: |
||
| 285 | self._header['Sum'] = splitted[1].replace('"', '').strip() |
||
| 286 | else: |
||
| 287 | self.warn('Unexpected header format', numline=self._numline) |
||
| 288 | |||
| 289 | return 0 |
||
| 290 | |||
| 291 | if line.startswith('Normalised to'): |
||
| 292 | if len(self._header) == 0: |
||
| 293 | self.err("No header found", numline=self._numline) |
||
| 294 | return -1 |
||
| 295 | |||
| 296 | splitted = self.splitLine(line) |
||
| 297 | if len(splitted) > 1: |
||
| 298 | self._header['Normalized'] = splitted[1].replace('"', '').strip() |
||
| 299 | else: |
||
| 300 | self.warn('Unexpected header format', numline=self._numline) |
||
| 301 | |||
| 302 | return 0 |
||
| 303 | |||
| 304 | View Code Duplication | if line.startswith('Sample type'): |
|
| 305 | if len(self._header) == 0: |
||
| 306 | self.err("No header found", numline=self._numline) |
||
| 307 | return -1 |
||
| 308 | |||
| 309 | splitted = self.splitLine(line) |
||
| 310 | if len(splitted) > 1: |
||
| 311 | self._header['Sample type'] = splitted[1].strip() |
||
| 312 | else: |
||
| 313 | self.warn('Unexpected header format', numline=self._numline) |
||
| 314 | |||
| 315 | return 0 |
||
| 316 | |||
| 317 | if line.startswith('Initial sample weight (g)'): |
||
| 318 | if len(self._header) == 0: |
||
| 319 | self.err("No header found", numline=self._numline) |
||
| 320 | return -1 |
||
| 321 | |||
| 322 | splitted = self.splitLine(line) |
||
| 323 | if len(splitted) > 1: |
||
| 324 | self._header['Initial sample weight'] = splitted[1].replace('"', '').strip() |
||
| 325 | else: |
||
| 326 | self.warn('Unexpected header format', numline=self._numline) |
||
| 327 | |||
| 328 | return 0 |
||
| 329 | |||
| 330 | if line.startswith('Weight after pressing (g)'): |
||
| 331 | if len(self._header) == 0: |
||
| 332 | self.err("No header found", numline=self._numline) |
||
| 333 | return -1 |
||
| 334 | |||
| 335 | splitted = self.splitLine(line) |
||
| 336 | if len(splitted) > 1: |
||
| 337 | self._header['Weight after pressing'] = splitted[1].replace('"', '').strip() |
||
| 338 | else: |
||
| 339 | self.warn('Unexpected header format', numline=self._numline) |
||
| 340 | |||
| 341 | return 0 |
||
| 342 | |||
| 343 | if line.startswith('Correction applied for medium'): |
||
| 344 | if len(self._header) == 0: |
||
| 345 | self.warn('Unexpected header format', numline=self._numline) |
||
| 346 | return -1 |
||
| 347 | |||
| 348 | splitted = self.splitLine(line) |
||
| 349 | if len(splitted) > 1: |
||
| 350 | self._header['Correction medium'] = splitted[1].replace('"', '').strip() |
||
| 351 | else: |
||
| 352 | self.warn('Unexpected header format', numline=self._numline) |
||
| 353 | |||
| 354 | return 0 |
||
| 355 | |||
| 356 | if line.startswith('Correction applied for film'): |
||
| 357 | if len(self._header) == 0: |
||
| 358 | self.err("No header found", numline=self._numline) |
||
| 359 | return -1 |
||
| 360 | |||
| 361 | splitted = self.splitLine(line) |
||
| 362 | if len(splitted) > 1: |
||
| 363 | self._header['Correction film'] = splitted[1].replace('"', '').strip() |
||
| 364 | else: |
||
| 365 | self.warn('Unexpected header format', numline=self._numline) |
||
| 366 | |||
| 367 | return 0 |
||
| 368 | |||
| 369 | if line.startswith('Used Compound list'): |
||
| 370 | if len(self._header) == 0: |
||
| 371 | self.err("No header found", numline=self._numline) |
||
| 372 | return -1 |
||
| 373 | |||
| 374 | splitted = self.splitLine(line) |
||
| 375 | if len(splitted) > 1: |
||
| 376 | self._header['Used compound'] = splitted[1].replace('"', '').strip() |
||
| 377 | else: |
||
| 378 | self.warn('Unexpected header format', numline=self._numline) |
||
| 379 | |||
| 380 | return 0 |
||
| 381 | if line.startswith('Results database:'): |
||
| 382 | if len(self._header) == 0: |
||
| 383 | self.err("No header found", numline=self._numline) |
||
| 384 | return -1 |
||
| 385 | |||
| 386 | splitted = self.splitLine(line) |
||
| 387 | if len(splitted) > 1: |
||
| 388 | self._header['Result database'] = splitted[1].replace('"', '').strip() |
||
| 389 | else: |
||
| 390 | self.warn('Unexpected header format', numline=self._numline) |
||
| 391 | |||
| 392 | return 0 |
||
| 393 | |||
| 394 | |||
| 395 | if self.columns_name: |
||
| 396 | if len(self._header) == 0: |
||
| 397 | self.err("No header found", numline=self._numline) |
||
| 398 | return -1 |
||
| 399 | |||
| 400 | #Grab column names |
||
| 401 | self._end_header = True |
||
| 402 | self._columns = self.splitLine(line) |
||
| 403 | return 1 |
||
| 404 | |||
| 405 | if line.startswith('Results database in'): |
||
| 406 | if len(self._header) == 0: |
||
| 407 | self.err("No header found", numline=self._numline) |
||
| 408 | return -1 |
||
| 409 | |||
| 410 | splitted = self.splitLine(line) |
||
| 411 | if len(splitted) > 1: |
||
| 412 | self._header['Database path'] = splitted[1]+splitted[2] |
||
| 413 | self.columns_name = True |
||
| 414 | else: |
||
| 415 | self.warn('Unexpected header format', numline=self._numline) |
||
| 416 | |||
| 417 | return 1 |
||
| 418 | |||
| 419 | else: |
||
| 420 | self._header['Date'] = line |
||
| 421 | return 1 |
||
| 422 | |||
| 523 |