| Conditions | 18 |
| Total Lines | 52 |
| Code Lines | 39 |
| 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 osm_poi_matchmaker.libs.opening_hours.OpeningHours.process() 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 | # -*- coding: utf-8 -*- |
||
| 97 | def process(self): |
||
| 98 | oh = '' |
||
| 99 | oh_list = [] |
||
| 100 | for k, v in self.df_dup.iterrows(): |
||
| 101 | if v['open'] is not None and v['close'] is not None: |
||
| 102 | # Order by week days |
||
| 103 | ordered = collections.OrderedDict(sorted(v['same'].items(), key=lambda x: x[0])) |
||
| 104 | same = list(ordered.values()) |
||
| 105 | # Public Holidays |
||
| 106 | if self.__public_holiday_open is None: |
||
| 107 | oh_ph = '' |
||
| 108 | elif self.__public_holiday_open is True: |
||
| 109 | oh_ph = '; PH on' |
||
| 110 | elif self.__public_holiday_open is False: |
||
| 111 | oh_ph = '; PH off' |
||
| 112 | else: |
||
| 113 | oh_ph = '' |
||
| 114 | # Try to merge days interval |
||
| 115 | if len(ordered) >= 2: |
||
| 116 | same_id = list(ordered.keys()) |
||
| 117 | diffs = [same_id[i + 1] - same_id[i] for i in range(len(same_id) - 1)] |
||
| 118 | # Diffs list contains only 1 to make day interval |
||
| 119 | if diffs.count(1) == len(diffs): |
||
| 120 | days = '{}-{}'.format(list(ordered.values())[0], list(ordered.values())[-1]) |
||
| 121 | # Make list of days |
||
| 122 | else: |
||
| 123 | days = ','.join(same) |
||
| 124 | # Make list of days |
||
| 125 | else: |
||
| 126 | days = ','.join(same) |
||
| 127 | if self.__lunch_break_start is None and self.__lunch_break_stop is None: |
||
| 128 | # If open and close are equals we handles as closed |
||
| 129 | if self.df_dup.at[k, 'open'] != self.df_dup.at[k, 'close']: |
||
| 130 | oh_list.append( |
||
| 131 | '{} {}-{}'.format(days.title(), self.df_dup.at[k, 'open'], self.df_dup.at[k, 'close'])) |
||
| 132 | else: |
||
| 133 | # If open and close are equals we handles as closed |
||
| 134 | if self.df_dup.at[k, 'open'] != self.df_dup.at[k, 'close']: |
||
| 135 | oh_list.append( |
||
| 136 | '{} {}-{},{}-{}'.format(days.title(), self.df_dup.at[k, 'open'], self.__lunch_break_start, |
||
| 137 | self.__lunch_break_stop, self.df_dup.at[k, 'close'])) |
||
| 138 | oh = '; '.join(oh_list) |
||
| 139 | oh = oh + oh_ph |
||
| 140 | if self.__non_stop is True or 'Mo-Su 00:00-24:00' in oh: |
||
| 141 | try: |
||
| 142 | return '24/7{}'.format(oh_ph) |
||
| 143 | except: |
||
| 144 | return '24/7' |
||
| 145 | elif oh_list == []: |
||
| 146 | return None |
||
| 147 | else: |
||
| 148 | return oh |
||
| 149 |