Conditions | 12 |
Total Lines | 54 |
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 prompt_for_config() 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 -*- |
||
180 | def prompt_for_config(context, no_input=False): |
||
181 | """ |
||
182 | Prompts the user to enter new config, using context as a source for the |
||
183 | field names and sample values. |
||
184 | |||
185 | :param no_input: Prompt the user at command line for manual configuration? |
||
186 | """ |
||
187 | cookiecutter_dict = {} |
||
188 | env = StrictEnvironment(context=context) |
||
189 | |||
190 | # First pass: Handle simple and raw variables, plus choices. |
||
191 | # These must be done first because the dictionaries keys and |
||
192 | # values might refer to them. |
||
193 | for key, raw in iteritems(context[u'cookiecutter']): |
||
194 | if key.startswith(u'_'): |
||
195 | cookiecutter_dict[key] = raw |
||
196 | continue |
||
197 | |||
198 | try: |
||
199 | if isinstance(raw, list): |
||
200 | # We are dealing with a choice variable |
||
201 | val = prompt_choice_for_config( |
||
202 | cookiecutter_dict, env, key, raw, no_input |
||
203 | ) |
||
204 | cookiecutter_dict[key] = val |
||
205 | elif not isinstance(raw, dict): |
||
206 | # We are dealing with a regular variable |
||
207 | val = render_variable(env, raw, cookiecutter_dict) |
||
208 | |||
209 | if not no_input: |
||
210 | val = read_user_variable(key, val) |
||
211 | |||
212 | cookiecutter_dict[key] = val |
||
213 | except UndefinedError as err: |
||
214 | msg = "Unable to render variable '{}'".format(key) |
||
215 | raise UndefinedVariableInTemplate(msg, err, context) |
||
216 | |||
217 | # Second pass; handle the dictionaries. |
||
218 | for key, raw in iteritems(context[u'cookiecutter']): |
||
219 | |||
220 | try: |
||
221 | if isinstance(raw, dict) and not key.startswith(u'_'): |
||
222 | # We are dealing with a dict variable |
||
223 | val = render_variable(env, raw, cookiecutter_dict) |
||
224 | |||
225 | if not no_input: |
||
226 | val = read_user_dict(key, val) |
||
227 | |||
228 | cookiecutter_dict[key] = val |
||
229 | except UndefinedError as err: |
||
230 | msg = "Unable to render variable '{}'".format(key) |
||
231 | raise UndefinedVariableInTemplate(msg, err, context) |
||
232 | |||
233 | return cookiecutter_dict |
||
234 |