| 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 | @staticmethod |
||
| 57 | def _get_timestamp_from_datestring(date_string): |
||
| 58 | """Convert the given date string to timestamp |
||
| 59 | |||
| 60 | :param date_string: |
||
| 61 | :return: |
||
| 62 | """ |
||
| 63 | if re.match('\d+.\d+.\d+ \d+:\d+:\d+', date_string): |
||
| 64 | return datetime.datetime.strptime(date_string, "%d.%m.%Y %H:%M:%S").timestamp() |
||
| 65 | elif re.match('\d+.\d+.\d+ \d+:\d+', date_string): |
||
| 66 | return datetime.datetime.strptime(date_string, "%d.%m.%Y %H:%M").timestamp() |
||
| 67 | elif re.match('\d+.\d+.\d+', date_string): |
||
| 68 | return datetime.datetime.strptime(date_string, "%d.%m.%Y").timestamp() |
||
| 69 | else: |
||
| 70 | raise AttributeError("The date doesn't fit the format: d.m.Y[ H:M[:S]]") |
||
| 71 | |||
| 72 | def apply(self, directory='', file_system_objects=None): |
||
| 73 | """Apply the filter values to the given FSOs(File System Objects) |
||
| 74 | |||
| 75 | :type directory: str |
||
| 76 | :type file_system_objects: list|tuple|Generator |
||
| 77 | :return: |
||
| 78 | """ |
||
| 79 | self._directory = directory |
||
| 80 | self._file_system_objects = file_system_objects |
||
| 81 | |||
| 82 | for file_system_object in self.file_system_objects: |
||
| 83 | abs_path = os.path.join(directory, file_system_object) |
||
| 84 | |||
| 85 | # check if the FSO exists, else we can't access the metadata |
||
| 86 | if not os.path.exists(abs_path): |
||
| 87 | continue |
||
| 88 | |||
| 89 | # check if the FSO is a file |
||
| 90 | if self._filter_assert_is_file and not os.path.isfile(abs_path): |
||
| 91 | continue |
||
| 92 | |||
| 93 | # check if the FSO is a folder |
||
| 94 | if self._filter_assert_is_folder and not os.path.isdir(abs_path): |
||
| 95 | continue |
||
| 96 | |||
| 97 | file_stats = os.stat(abs_path) |
||
| 98 | |||
| 99 | # check if the FSO creation date matches the constraint |
||
| 100 | if self._filter_creation_date and not self.apply_creation_date(file_stats): |
||
| 101 | continue |
||
| 102 | |||
| 103 | # check if the FSO modification date matches the constraint |
||
| 104 | if self._filter_modified_date and not self.apply_modified_date(file_stats): |
||
| 105 | continue |
||
| 106 | |||
| 107 | # check if the FSO name matches the constraint |
||
| 108 | if self._filter_name and not self._filter_name.cmp_func(file_system_object, self._filter_name.value): |
||
| 109 | continue |
||
| 110 | |||
| 111 | # check if the FSO suffix matches the constraint |
||
| 112 | if self._filter_file_type and not self._filter_file_type.cmp_func(Path(abs_path).suffix, |
||
| 113 | self._filter_file_type.value): |
||
| 114 | continue |
||
| 115 | |||
| 116 | # check if the FSO size matches the constraint |
||
| 148 |