| Conditions | 6 |
| Total Lines | 134 |
| Code Lines | 81 |
| 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:
| 1 | #!/usr/bin/env python3 |
||
| 154 | def fill_uid_samples(submission_obj, template): |
||
| 155 | # debug |
||
| 156 | logger.info("called fill_uid_samples()") |
||
| 157 | |||
| 158 | # get language |
||
| 159 | language = submission_obj.gene_bank_country.label |
||
| 160 | |||
| 161 | # iterate among excel template |
||
| 162 | for record in template.get_sample_records(): |
||
| 163 | # get animal by reading record |
||
| 164 | animal_record = template.get_animal_from_sample(record) |
||
| 165 | |||
| 166 | # get specie (mind synonyms) |
||
| 167 | specie = DictSpecie.get_specie_check_synonyms( |
||
| 168 | species_label=animal_record.species, |
||
| 169 | language=language) |
||
| 170 | |||
| 171 | logger.debug("Found '%s' as specie" % (specie)) |
||
| 172 | |||
| 173 | # get breed from animal record |
||
| 174 | breed_record = template.get_breed_from_animal(animal_record) |
||
| 175 | |||
| 176 | # get a country for this breed |
||
| 177 | country = DictCountry.objects.get( |
||
| 178 | label=breed_record.efabis_breed_country) |
||
| 179 | |||
| 180 | # ok get a real dictbreed object |
||
| 181 | breed = DictBreed.objects.get( |
||
| 182 | supplied_breed=breed_record.supplied_breed, |
||
| 183 | specie=specie, |
||
| 184 | country=country) |
||
| 185 | |||
| 186 | logger.debug("Selected breed is %s" % (breed)) |
||
| 187 | |||
| 188 | animal = Animal.objects.get( |
||
| 189 | name=animal_record.animal_id_in_data_source, |
||
| 190 | breed=breed, |
||
| 191 | owner=submission_obj.owner) |
||
| 192 | |||
| 193 | logger.debug("Selected animal is %s" % (animal)) |
||
| 194 | |||
| 195 | # get a organism part. Organism parts need to be in lowercases |
||
| 196 | organism_part = get_or_create_obj( |
||
| 197 | DictUberon, |
||
| 198 | label=record.organism_part |
||
| 199 | ) |
||
| 200 | |||
| 201 | # get developmental_stage and physiological_stage terms |
||
| 202 | # they are not mandatory |
||
| 203 | devel_stage, physio_stage = None, None |
||
| 204 | |||
| 205 | if record.developmental_stage: |
||
| 206 | devel_stage = get_or_create_obj( |
||
| 207 | DictDevelStage, |
||
| 208 | label=record.developmental_stage |
||
| 209 | ) |
||
| 210 | |||
| 211 | if record.physiological_stage: |
||
| 212 | physio_stage = get_or_create_obj( |
||
| 213 | DictPhysioStage, |
||
| 214 | label=record.physiological_stage |
||
| 215 | ) |
||
| 216 | |||
| 217 | # animal age could be present or not |
||
| 218 | if record.animal_age_at_collection: |
||
| 219 | animal_age_at_collection, time_units = parse_image_timedelta( |
||
| 220 | record.animal_age_at_collection) |
||
| 221 | |||
| 222 | else: |
||
| 223 | # derive animal age at collection |
||
| 224 | animal_age_at_collection, time_units = image_timedelta( |
||
| 225 | record.collection_date, animal.birth_date) |
||
| 226 | |||
| 227 | # another time column |
||
| 228 | preparation_interval, preparation_interval_units = None, None |
||
| 229 | |||
| 230 | if record.sampling_to_preparation_interval: |
||
| 231 | preparation_interval, preparation_interval_units = \ |
||
| 232 | parse_image_timedelta(record.sampling_to_preparation_interval) |
||
| 233 | |||
| 234 | # now get accuracy |
||
| 235 | accuracy = ACCURACIES.get_value_by_desc( |
||
| 236 | record.collection_place_accuracy) |
||
| 237 | |||
| 238 | # now get storage and storage processing |
||
| 239 | # TODO; check those values in excel columns |
||
| 240 | storage = SAMPLE_STORAGE.get_value_by_desc( |
||
| 241 | record.sample_storage) |
||
| 242 | |||
| 243 | storage_processing = SAMPLE_STORAGE_PROCESSING.get_value_by_desc( |
||
| 244 | record.sample_storage_processing) |
||
| 245 | |||
| 246 | # create a new object. Using defaults to avoid collisions when |
||
| 247 | # updating data |
||
| 248 | defaults = { |
||
| 249 | 'alternative_id': record.alternative_sample_id, |
||
| 250 | 'description': record.sample_description, |
||
| 251 | 'protocol': record.specimen_collection_protocol, |
||
| 252 | 'collection_date': record.collection_date, |
||
| 253 | 'collection_place_latitude': record.collection_place_latitude, |
||
| 254 | 'collection_place_longitude': record.collection_place_longitude, |
||
| 255 | 'collection_place': record.collection_place, |
||
| 256 | 'collection_place_accuracy': accuracy, |
||
| 257 | 'organism_part': organism_part, |
||
| 258 | 'developmental_stage': devel_stage, |
||
| 259 | 'physiological_stage': physio_stage, |
||
| 260 | 'animal_age_at_collection': animal_age_at_collection, |
||
| 261 | 'animal_age_at_collection_units': time_units, |
||
| 262 | 'availability': record.availability, |
||
| 263 | 'storage': storage, |
||
| 264 | 'storage_processing': storage_processing, |
||
| 265 | 'preparation_interval': preparation_interval, |
||
| 266 | 'preparation_interval_units': preparation_interval_units, |
||
| 267 | } |
||
| 268 | |||
| 269 | update_or_create_obj( |
||
| 270 | Sample, |
||
| 271 | name=record.sample_id_in_data_source, |
||
| 272 | animal=animal, |
||
| 273 | owner=submission_obj.owner, |
||
| 274 | submission=submission_obj, |
||
| 275 | defaults=defaults) |
||
| 276 | |||
| 277 | # create a validation summary object and set all_count |
||
| 278 | validation_summary = get_or_create_obj( |
||
| 279 | ValidationSummary, |
||
| 280 | submission=submission_obj, |
||
| 281 | type="sample") |
||
| 282 | |||
| 283 | # reset counts |
||
| 284 | validation_summary.reset_all_count() |
||
| 285 | |||
| 286 | # debug |
||
| 287 | logger.info("fill_uid_samples() completed") |
||
| 288 | |||
| 397 |