Conditions | 10 |
Total Lines | 100 |
Code Lines | 74 |
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 config.model_telegram.TelegramWord.keyboard_cpx() 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 -*- |
||
106 | def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): |
||
107 | """ |
||
108 | :return: |
||
109 | """ |
||
110 | |||
111 | kb_cpx_close = kb_close() |
||
112 | |||
113 | def keyboard_navi(index_start, index_end, delimiter): |
||
114 | |||
115 | text_arrow_back = "\U0000276E" * 2 |
||
116 | text_arrow_forward = "\U0000276F" * 2 |
||
117 | button_back, button_forward = None, None |
||
118 | |||
119 | common_data = { |
||
120 | mark_entity: entity_predy, |
||
121 | mark_action: action_predy_kb_cpx_show, |
||
122 | mark_record_id: self.id, |
||
123 | } |
||
124 | |||
125 | if index_start != 0: |
||
126 | cbd_predy_kb_cpx_back = { |
||
127 | **common_data, mark_slice_start: index_start - delimiter, } |
||
128 | button_back = { |
||
129 | t: text_arrow_back, |
||
130 | cbd: callback_from_info(cbd_predy_kb_cpx_back)} |
||
131 | |||
132 | if index_end != len(self.complexes): |
||
133 | cbd_predy_kb_cpx_forward = { |
||
134 | **common_data, mark_slice_start: index_end, } |
||
135 | button_forward = { |
||
136 | t: text_arrow_forward, |
||
137 | cbd: callback_from_info(cbd_predy_kb_cpx_forward)} |
||
138 | |||
139 | nav_row = [b for b in [button_back, button_forward] if b] |
||
140 | return Keyboa(nav_row, items_in_row=2)() |
||
141 | |||
142 | def keyboard_data(current_complexes): |
||
143 | cpx_items = [{t: cpx.name, cbd: callback_from_info({ |
||
144 | mark_entity: entity_predy, |
||
145 | mark_action: action_predy_send_card, |
||
146 | mark_record_id: cpx.id, })} for cpx in current_complexes] |
||
147 | return Keyboa(items=cpx_items, alignment=True, items_in_row=4)() |
||
148 | |||
149 | def keyboard_hide(total_number_of_complexes): |
||
150 | text_cpx_hide = f"Hide Complex{'es' if total_number_of_complexes > 1 else ''}" |
||
151 | cbd_predy_kb_cpx_hide = { |
||
152 | mark_entity: entity_predy, |
||
153 | mark_action: action_predy_kb_cpx_hide, |
||
154 | mark_record_id: self.id, } |
||
155 | button_predy_kb_cpx_hide = [{ |
||
156 | t: text_cpx_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)}, ] |
||
157 | return Keyboa(button_predy_kb_cpx_hide)() |
||
158 | |||
159 | def keyboard_show(total_number_of_complexes: int): |
||
160 | text_cpx_show = f"Show Complex{'es' if total_number_of_complexes > 1 else ''} ({total_number_of_complexes})" |
||
161 | cbd_predy_kb_cpx_show = { |
||
162 | mark_entity: entity_predy, |
||
163 | mark_action: action_predy_kb_cpx_show, |
||
164 | mark_record_id: self.id, } |
||
165 | button_show = [{ |
||
166 | t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)}, ] |
||
167 | return Keyboa.combine((Keyboa(button_show)(), kb_cpx_close)) |
||
168 | |||
169 | def get_delimiter(total_number_of_complexes: int): |
||
170 | from bot import MIN_NUMBER_OF_BUTTONS |
||
171 | allowed_range = list(range(MIN_NUMBER_OF_BUTTONS, MIN_NUMBER_OF_BUTTONS + 11)) |
||
172 | lst = [(total_number_of_complexes % i, i) for i in allowed_range] |
||
173 | delimiter = min(lst, key=lambda x: abs(x[0] - MIN_NUMBER_OF_BUTTONS))[1] |
||
174 | for i in lst: |
||
175 | if i[0] == 0: |
||
176 | delimiter = i[1] |
||
177 | break |
||
178 | return delimiter |
||
179 | |||
180 | total_num_of_cpx = len(self.complexes) |
||
181 | |||
182 | if not total_num_of_cpx: |
||
183 | return kb_cpx_close |
||
184 | |||
185 | if not show_list: |
||
186 | return keyboard_show(total_num_of_cpx) |
||
187 | |||
188 | current_delimiter = get_delimiter(total_num_of_cpx) |
||
189 | |||
190 | kb_cpx_hide = keyboard_hide(total_num_of_cpx) |
||
191 | |||
192 | last_allowed_element = slice_start + current_delimiter |
||
193 | slice_end = last_allowed_element if last_allowed_element < total_num_of_cpx else total_num_of_cpx |
||
194 | |||
195 | current_cpx_set = self.complexes[slice_start:slice_end] |
||
196 | kb_cpx_data = keyboard_data(current_cpx_set) |
||
197 | |||
198 | kb_cpx_nav = None |
||
199 | |||
200 | if total_num_of_cpx > current_delimiter: |
||
201 | kb_cpx_nav = keyboard_navi(slice_start, slice_end, current_delimiter) |
||
202 | |||
203 | kb_combo = (kb_cpx_hide, kb_cpx_data, kb_cpx_nav, kb_cpx_close) |
||
204 | |||
205 | return Keyboa.combine(kb_combo) |
||
206 | |||
219 |