Conditions | 12 |
Total Lines | 53 |
Code Lines | 38 |
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 send-tasks.gmp.parse_send_xml_tree() 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 -*- |
||
119 | def parse_send_xml_tree(gmp, xml_tree): |
||
120 | task_xml_elements = xml_tree.xpath('task') |
||
121 | print(task_xml_elements) |
||
122 | if not task_xml_elements: |
||
123 | error_and_exit("No tasks found.") |
||
124 | tasks = [] |
||
125 | for task in task_xml_elements: |
||
126 | keywords = {'name': task.find('name').text} |
||
127 | |||
128 | if task.find('comment').text is not None: |
||
129 | keywords['comment'] = task.find('comment').text |
||
130 | |||
131 | interactive_options(gmp, task, keywords) |
||
|
|||
132 | |||
133 | new_task = gmp.create_task(**keywords) |
||
134 | |||
135 | mod_keywords = {'task_id': new_task.xpath('//@id')[0]} |
||
136 | tasks.append(mod_keywords['task_id']) |
||
137 | |||
138 | if task.find('schedule_periods') is not None: |
||
139 | mod_keywords['schedule_periods'] = int( |
||
140 | task.find('schedule_periods').text |
||
141 | ) |
||
142 | |||
143 | if task.find('observers').text: |
||
144 | mod_keywords['observers'] = task.find('observers').text |
||
145 | |||
146 | if task.xpath('schedule/@id')[0]: |
||
147 | mod_keywords['schedule_id'] = task.xpath('schedule/@id')[0] |
||
148 | |||
149 | if task.xpath('preferences/preference'): |
||
150 | preferences, scanner_name_list, value_list = {}, [], [] |
||
151 | |||
152 | for preference in task.xpath('preferences/preference'): |
||
153 | scanner_name_list.append(preference.find('scanner_name').text) |
||
154 | if preference.find('value').text is not None: |
||
155 | value_list.append(preference.find('value').text) |
||
156 | else: |
||
157 | value_list.append('') |
||
158 | preferences['scanner_name'] = scanner_name_list |
||
159 | preferences['value'] = value_list |
||
160 | mod_keywords['preferences'] = preferences |
||
161 | |||
162 | if task.xpath('file/@name'): |
||
163 | file = dict( |
||
164 | name=task.xpath('file/@name'), action=task.xpath('file/@action') |
||
165 | ) |
||
166 | |||
167 | mod_keywords['file'] = file |
||
168 | |||
169 | if len(mod_keywords) > 1: |
||
170 | gmp.modify_task(**mod_keywords) |
||
171 | return tasks |
||
172 | |||
229 |