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