| Conditions | 11 |
| Total Lines | 55 |
| Code Lines | 37 |
| 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 asgardpy.io.input_dl3.DL3Files.select_unique_files() 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 | """ |
||
| 120 | |||
| 121 | def select_unique_files(self, key, file_list): |
||
| 122 | """ |
||
| 123 | Select Unique files from all of the provided LAT files, as per the |
||
| 124 | given key. If there are no distinct key types of files, the value is None. |
||
| 125 | """ |
||
| 126 | # Have to make more checks or add conditions on selecting only select |
||
| 127 | # files instead from the glob-searched lists. |
||
| 128 | if self.dl3_type.lower() in ["lat"]: |
||
| 129 | var_list = [ |
||
| 130 | "events_files", |
||
| 131 | "edrm_files", |
||
| 132 | "expmap_files", |
||
| 133 | "psf_files", |
||
| 134 | ] |
||
| 135 | file_list["xml_file"] = self.xml_files[0] |
||
| 136 | |||
| 137 | if self.dl3_type.lower() == "lat-aux": |
||
| 138 | var_list = [] |
||
| 139 | if key: |
||
| 140 | if "0" not in key: # For fermipy files, the diffuse files are already unique |
||
| 141 | var_list = [ |
||
| 142 | "iso_diff_files", |
||
| 143 | ] |
||
| 144 | if isinstance(self.iso_diff_files, list): |
||
| 145 | self.iso_gal_f = self.iso_diff_files[0] |
||
| 146 | else: |
||
| 147 | self.iso_gal_f = self.iso_diff_files |
||
| 148 | file_list["iso_diff_file"] = self.iso_gal_f |
||
| 149 | |||
| 150 | if isinstance(self.gal_diff_files, list): |
||
| 151 | self.diff_gal_f = self.gal_diff_files[0] |
||
| 152 | else: |
||
| 153 | self.diff_gal_f = self.gal_diff_files |
||
| 154 | file_list["gal_diff_file"] = self.diff_gal_f |
||
| 155 | |||
| 156 | if len(var_list) > 0: |
||
| 157 | for _v in var_list: |
||
| 158 | if key is not None: |
||
| 159 | filtered = [K for K in getattr(self, _v) if key in str(K.name)] |
||
| 160 | if len(filtered) == 1: |
||
| 161 | self.log.info("Selecting the file with name containing %s", key) |
||
| 162 | setattr(self, _v.replace("_files", "_f"), filtered[0]) |
||
| 163 | else: |
||
| 164 | raise ValueError( |
||
| 165 | "Variable {%s} does not contain one element after filtering by {%s}", |
||
| 166 | getattr(self, _v), |
||
| 167 | key, |
||
| 168 | ) |
||
| 169 | else: |
||
| 170 | self.log.info("No distinct key provided, selecting the first file in the list") |
||
| 171 | setattr(self, _v.replace("_files", "_f"), getattr(self, _v)[0]) |
||
| 172 | |||
| 173 | file_list[_v.replace("files", "file")] = getattr(self, _v.replace("_files", "_f")) |
||
| 174 | |||
| 175 | return file_list |
||
| 176 |