| Conditions | 11 |
| Total Lines | 137 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| 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 excel.helpers.fill_uid.fill_uid_samples() 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 python3 |
||
| 232 | def fill_uid_samples(submission_obj, template): |
||
| 233 | # debug |
||
| 234 | logger.info("called fill_uid_samples()") |
||
| 235 | |||
| 236 | # iterate among excel template |
||
| 237 | for record in template.get_sample_records(): |
||
| 238 | # get name for this sample |
||
| 239 | name = Name.objects.get( |
||
| 240 | name=record.sample_id_in_data_source, |
||
| 241 | submission=submission_obj, |
||
| 242 | owner=submission_obj.owner) |
||
| 243 | |||
| 244 | # get animal by reading record |
||
| 245 | animal = Animal.objects.get( |
||
| 246 | name__name=record.animal_id_in_data_source, |
||
| 247 | name__submission=submission_obj) |
||
| 248 | |||
| 249 | # get a organism part. Organism parts need to be in lowercases |
||
| 250 | organism_part, created = DictUberon.objects.get_or_create( |
||
| 251 | label=record.organism_part |
||
| 252 | ) |
||
| 253 | |||
| 254 | if created: |
||
| 255 | logger.info("Created %s" % organism_part) |
||
| 256 | |||
| 257 | else: |
||
| 258 | logger.debug("Found %s" % organism_part) |
||
| 259 | |||
| 260 | # get developmental_stage and physiological_stage terms |
||
| 261 | # they are not mandatory |
||
| 262 | if record.developmental_stage: |
||
| 263 | devel_stage, created = DictDevelStage.objects.get_or_create( |
||
| 264 | label=record.developmental_stage |
||
| 265 | ) |
||
| 266 | |||
| 267 | if created: |
||
| 268 | logger.info("Created %s" % devel_stage) |
||
| 269 | |||
| 270 | else: |
||
| 271 | logger.debug("Found %s" % devel_stage) |
||
| 272 | |||
| 273 | else: |
||
| 274 | devel_stage = None |
||
| 275 | |||
| 276 | if record.physiological_stage: |
||
| 277 | physio_stage, created = DictPhysioStage.objects.get_or_create( |
||
| 278 | label=record.physiological_stage |
||
| 279 | ) |
||
| 280 | |||
| 281 | if created: |
||
| 282 | logger.info("Created %s" % physio_stage) |
||
| 283 | |||
| 284 | else: |
||
| 285 | logger.debug("Found %s" % physio_stage) |
||
| 286 | |||
| 287 | else: |
||
| 288 | physio_stage = None |
||
| 289 | |||
| 290 | # animal age could be present or not |
||
| 291 | if record.animal_age_at_collection: |
||
| 292 | animal_age_at_collection, time_units = parse_image_timedelta( |
||
| 293 | record.animal_age_at_collection) |
||
| 294 | |||
| 295 | else: |
||
| 296 | # derive animal age at collection |
||
| 297 | animal_age_at_collection, time_units = image_timedelta( |
||
| 298 | record.collection_date, animal.birth_date) |
||
| 299 | |||
| 300 | # another time column |
||
| 301 | if record.sampling_to_preparation_interval: |
||
| 302 | preparation_interval, preparation_interval_units = \ |
||
| 303 | parse_image_timedelta(record.sampling_to_preparation_interval) |
||
| 304 | |||
| 305 | else: |
||
| 306 | preparation_interval, preparation_interval_units = None, None |
||
| 307 | |||
| 308 | # now get accuracy |
||
| 309 | accuracy = ACCURACIES.get_value_by_desc( |
||
| 310 | record.collection_place_accuracy) |
||
| 311 | |||
| 312 | # now get storage and storage processing |
||
| 313 | # TODO; check those values in excel columns |
||
| 314 | storage = SAMPLE_STORAGE.get_value_by_desc( |
||
| 315 | record.sample_storage) |
||
| 316 | |||
| 317 | storage_processing = SAMPLE_STORAGE_PROCESSING.get_value_by_desc( |
||
| 318 | record.sample_storage_processing) |
||
| 319 | |||
| 320 | # create a new object. Using defaults to avoid collisions when |
||
| 321 | # updating data |
||
| 322 | defaults = { |
||
| 323 | 'alternative_id': record.alternative_sample_id, |
||
| 324 | 'description': record.sample_description, |
||
| 325 | 'animal': animal, |
||
| 326 | 'protocol': record.specimen_collection_protocol, |
||
| 327 | 'collection_date': record.collection_date, |
||
| 328 | 'collection_place_latitude': record.collection_place_latitude, |
||
| 329 | 'collection_place_longitude': record.collection_place_longitude, |
||
| 330 | 'collection_place': record.collection_place, |
||
| 331 | 'collection_place_accuracy': accuracy, |
||
| 332 | 'organism_part': organism_part, |
||
| 333 | 'developmental_stage': devel_stage, |
||
| 334 | 'physiological_stage': physio_stage, |
||
| 335 | 'animal_age_at_collection': animal_age_at_collection, |
||
| 336 | 'animal_age_at_collection_units': time_units, |
||
| 337 | 'availability': record.availability, |
||
| 338 | 'storage': storage, |
||
| 339 | 'storage_processing': storage_processing, |
||
| 340 | 'preparation_interval': preparation_interval, |
||
| 341 | 'preparation_interval_units': preparation_interval_units, |
||
| 342 | 'owner': submission_obj.owner, |
||
| 343 | } |
||
| 344 | |||
| 345 | sample, created = Sample.objects.update_or_create( |
||
| 346 | name=name, |
||
| 347 | defaults=defaults) |
||
| 348 | |||
| 349 | if created: |
||
| 350 | logger.debug("Created %s" % sample) |
||
| 351 | |||
| 352 | else: |
||
| 353 | logger.debug("Updating %s" % sample) |
||
| 354 | |||
| 355 | # create a validation summary object and set all_count |
||
| 356 | validation_summary, created = ValidationSummary.objects.get_or_create( |
||
| 357 | submission=submission_obj, type="sample") |
||
| 358 | |||
| 359 | if created: |
||
| 360 | logger.debug( |
||
| 361 | "ValidationSummary animal created for submission %s" % |
||
| 362 | submission_obj) |
||
| 363 | |||
| 364 | # reset counts |
||
| 365 | validation_summary.reset_all_count() |
||
| 366 | |||
| 367 | # debug |
||
| 368 | logger.info("fill_uid_samples() completed") |
||
| 369 | |||
| 458 |