Conditions | 21 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 _placeholders_recursif() 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 -*- |
||
65 | def _placeholders_recursif(nodelist, plist, blist): |
||
66 | """Recursively search into a template node list for PlaceholderNode |
||
67 | node.""" |
||
68 | # I needed to do this lazy import to compile the documentation |
||
69 | from django.template.loader_tags import BlockNode |
||
70 | |||
71 | if len(blist): |
||
72 | block = blist[-1] |
||
73 | else: |
||
74 | block = None |
||
75 | |||
76 | for node in nodelist: |
||
77 | |||
78 | if isinstance(node, BlockNode): |
||
79 | if node not in blist: |
||
80 | blist.append(node) |
||
81 | if not block: |
||
82 | block = node |
||
83 | |||
84 | if block: |
||
85 | if isinstance(node, template.base.VariableNode): |
||
86 | if(node.filter_expression.var.var == u'block.super'): |
||
87 | block.has_super_var = True |
||
88 | |||
89 | # extends node? |
||
90 | if hasattr(node, 'parent_name'): |
||
91 | # I do not know why I did this... but the tests are guarding it |
||
92 | dummy_context2 = Context() |
||
93 | dummy_context2.template = template.Template("") |
||
94 | _placeholders_recursif(node.get_parent(dummy_context2).nodelist, |
||
95 | plist, blist) |
||
96 | # include node? |
||
97 | elif hasattr(node, 'template') and hasattr(node.template, 'nodelist'): |
||
98 | _placeholders_recursif(node.template.nodelist, plist, blist) |
||
99 | |||
100 | # Is it a placeholder? |
||
101 | if hasattr(node, 'page') and hasattr(node, 'parsed') and \ |
||
102 | hasattr(node, 'as_varname') and hasattr(node, 'name') \ |
||
103 | and hasattr(node, 'section'): |
||
104 | if block: |
||
105 | node.found_in_block = block |
||
106 | plist.append(node) |
||
107 | node.render(dummy_context) |
||
108 | |||
109 | for key in ('nodelist', 'nodelist_true', 'nodelist_false'): |
||
110 | |||
111 | if hasattr(node, key): |
||
112 | try: |
||
113 | _placeholders_recursif(getattr(node, key), plist, blist) |
||
114 | except: |
||
115 | pass |
||
116 | |||
159 |