Conditions | 12 |
Total Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 xmlrenderer() 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 | # |
||
179 | def xmlrenderer(data, **kwargs): # pylint: disable=unused-argument |
||
180 | """Output as XML |
||
181 | |||
182 | :param list data: Filename with properties |
||
183 | syntax: [(FILENAME, {PROPERTIES}), ...] |
||
184 | :param dict kwargs: for further customizations |
||
185 | :return: rendered output |
||
186 | :rtype: str |
||
187 | """ |
||
188 | |||
189 | root = etree.Element("docmanager") |
||
190 | tree = root.getroottree() |
||
191 | |||
192 | args = kwargs["args"] |
||
193 | index = 0 |
||
194 | |||
195 | if args.action == "alias": |
||
196 | aliaseselem = etree.Element("aliases") |
||
197 | root.append(aliaseselem) |
||
198 | |||
199 | for name in data['aliases'].keys(): |
||
200 | value = data['aliases'][name] |
||
201 | |||
202 | root[0].append(etree.Element("alias")) |
||
203 | |||
204 | child = root[0][index] |
||
205 | child.set("name", name) |
||
206 | |||
207 | child.text = value |
||
208 | |||
209 | index += 1 |
||
210 | |||
211 | elif args.action == "get": |
||
212 | fileselem = etree.Element("files") |
||
213 | root.append(fileselem) |
||
214 | |||
215 | for i in data['data']: |
||
216 | if len(i[1]): |
||
217 | filename = i[0] |
||
218 | |||
219 | root[0].append(etree.Element("file")) |
||
220 | |||
221 | child = root[0][index] |
||
222 | child.set("name", filename) |
||
223 | |||
224 | for x in i[1]: |
||
225 | prop = x |
||
226 | value = i[1][x] |
||
227 | |||
228 | elem = etree.Element("property") |
||
229 | elem.set("name", prop) |
||
230 | elem.text = value |
||
231 | |||
232 | child.append(elem) |
||
233 | |||
234 | index += 1 |
||
235 | |||
236 | elif args.action == "get_attr": |
||
237 | """ |
||
238 | Output structure: |
||
239 | <!DOCTYPE docmanager> |
||
240 | <docmanager> |
||
241 | <files> |
||
242 | <file name="example.xml"> |
||
243 | <property name="priority/test/hello"> |
||
244 | <attribute name="someattribute">Hello</attribute> |
||
245 | <attribute name="attr2">Hallo</attribute> |
||
246 | </property> |
||
247 | <property name="status"> |
||
248 | <attribute name="attr3">Hi</attribute> |
||
249 | </property> |
||
250 | </file> |
||
251 | </files> |
||
252 | </docmanager> |
||
253 | """ |
||
254 | |||
255 | fileselem = etree.Element("files") |
||
256 | root.append(fileselem) |
||
257 | |||
258 | if data['data']: |
||
259 | index = 0 |
||
260 | |||
261 | for i in data['data']: |
||
262 | filename = i |
||
263 | root[0].append(etree.Element("file")) |
||
264 | |||
265 | child = root[0][index] |
||
266 | child.set("name", filename) |
||
267 | |||
268 | for prop in data['data'][i]: |
||
269 | propelem = etree.Element("property") |
||
270 | child.append(propelem) |
||
271 | propelem.set("name", prop) |
||
272 | |||
273 | for key, value in data['data'][i][prop].items(): |
||
274 | elem = etree.Element("attribute") |
||
275 | elem.set("name", key) |
||
276 | elem.text = value |
||
277 | |||
278 | propelem.append(elem) |
||
279 | |||
280 | index += 1 |
||
281 | |||
282 | print(etree.tostring(tree, |
||
283 | encoding="unicode", |
||
284 | pretty_print=True, |
||
285 | doctype="<!DOCTYPE docmanager>")) |
||
286 | |||
333 |