| Conditions | 19 |
| Total Lines | 60 |
| Code Lines | 34 |
| 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 saucenao.files.filter.Filter.apply() 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/python |
||
| 56 | def apply(self, directory='', file_system_objects=None): |
||
| 57 | """Apply the filter values to the given FSOs(File System Objects) |
||
| 58 | |||
| 59 | :type directory: str |
||
| 60 | :type file_system_objects: list|tuple|Generator |
||
| 61 | :return: |
||
| 62 | """ |
||
| 63 | if file_system_objects is None: |
||
| 64 | if directory: |
||
| 65 | file_system_objects = os.listdir(directory) |
||
| 66 | else: |
||
| 67 | file_system_objects = [] |
||
| 68 | |||
| 69 | for file_system_object in file_system_objects: |
||
| 70 | abs_path = os.path.join(directory, file_system_object) |
||
| 71 | |||
| 72 | # check if the FSO exists, else we can't access the metadata |
||
| 73 | if not os.path.exists(abs_path): |
||
| 74 | continue |
||
| 75 | |||
| 76 | # check if the FSO is a file |
||
| 77 | if self.filter_assert_is_file and not os.path.isfile(abs_path): |
||
| 78 | continue |
||
| 79 | |||
| 80 | # check if the FSO is a folder |
||
| 81 | if self.filter_assert_is_folder and not os.path.isdir(abs_path): |
||
| 82 | continue |
||
| 83 | |||
| 84 | file_stats = os.stat(abs_path) |
||
| 85 | |||
| 86 | # check if the FSO creation date matches the constraint |
||
| 87 | if self.filter_creation_date: |
||
| 88 | creation_time = file_stats[ST_CTIME] |
||
| 89 | expected_creation_time = self._get_timestamp_from_datestring(self.filter_creation_date.value) |
||
| 90 | if not self.filter_creation_date.cmp_func(creation_time, expected_creation_time): |
||
| 91 | continue |
||
| 92 | |||
| 93 | # check if the FSO modification date matches the constraint |
||
| 94 | if self.filter_modified_date: |
||
| 95 | modified_time = file_stats[ST_MTIME] |
||
| 96 | expected_modified_time = self._get_timestamp_from_datestring(self.filter_modified_date.value) |
||
| 97 | if not self.filter_modified_date.cmp_func(modified_time, expected_modified_time): |
||
| 98 | continue |
||
| 99 | |||
| 100 | # check if the FSO name matches the constraint |
||
| 101 | if self.filter_name: |
||
| 102 | if not self.filter_name.cmp_func(file_system_object, self.filter_name.value): |
||
| 103 | continue |
||
| 104 | |||
| 105 | # check if the FSO suffix matches the constraint |
||
| 106 | if self.filter_file_type: |
||
| 107 | if not self.filter_file_type.cmp_func(Path(abs_path).suffix, self.filter_file_type.value): |
||
| 108 | continue |
||
| 109 | |||
| 110 | # check if the FSO size matches the constraint |
||
| 111 | if self.filter_file_size: |
||
| 112 | if not self.filter_file_size.cmp_func(file_stats[ST_SIZE], self.filter_file_size.value): |
||
| 113 | continue |
||
| 114 | |||
| 115 | yield file_system_object |
||
| 116 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.