Conditions | 18 |
Total Lines | 67 |
Code Lines | 41 |
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 cookiecutter.prompt.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 | """Functions for prompting the user for project info.""" |
||
176 | def prompt_for_config(context, no_input=False): |
||
177 | """Prompt user to enter a new config. |
||
178 | |||
179 | :param dict context: Source for field names and sample values. |
||
180 | :param no_input: Do not prompt for user input and use only values from context. |
||
181 | """ |
||
182 | cookiecutter_dict = OrderedDict([]) |
||
183 | env = StrictEnvironment(context=context) |
||
184 | |||
185 | # First pass: Handle simple and raw variables, plus choices. |
||
186 | # These must be done first because the dictionaries keys and |
||
187 | # values might refer to them. |
||
188 | for key, raw in context['cookiecutter'].items(): |
||
189 | if key.startswith('_') and not key.startswith('__'): |
||
190 | cookiecutter_dict[key] = raw |
||
191 | continue |
||
192 | elif key.startswith('__'): |
||
193 | cookiecutter_dict[key] = render_variable(env, raw, cookiecutter_dict) |
||
194 | continue |
||
195 | |||
196 | try: |
||
197 | if isinstance(raw, list): |
||
198 | # We are dealing with a choice variable |
||
199 | val = prompt_choice_for_config( |
||
200 | cookiecutter_dict, env, key, raw, no_input |
||
201 | ) |
||
202 | cookiecutter_dict[key] = val |
||
203 | elif isinstance(raw, bool): |
||
204 | # We are dealing with a boolean variable |
||
205 | if no_input: |
||
206 | cookiecutter_dict[key] = render_variable( |
||
207 | env, raw, cookiecutter_dict |
||
208 | ) |
||
209 | else: |
||
210 | cookiecutter_dict[key] = read_user_yes_no(key, raw) |
||
211 | elif not isinstance(raw, dict): |
||
212 | # We are dealing with a regular variable |
||
213 | val = render_variable(env, raw, cookiecutter_dict) |
||
214 | |||
215 | if not no_input: |
||
216 | val = read_user_variable(key, val) |
||
217 | |||
218 | cookiecutter_dict[key] = val |
||
219 | except UndefinedError as err: |
||
220 | msg = f"Unable to render variable '{key}'" |
||
221 | raise UndefinedVariableInTemplate(msg, err, context) from err |
||
222 | |||
223 | # Second pass; handle the dictionaries. |
||
224 | for key, raw in context['cookiecutter'].items(): |
||
225 | # Skip private type dicts not to be rendered. |
||
226 | if key.startswith('_') and not key.startswith('__'): |
||
227 | continue |
||
228 | |||
229 | try: |
||
230 | if isinstance(raw, dict): |
||
231 | # We are dealing with a dict variable |
||
232 | val = render_variable(env, raw, cookiecutter_dict) |
||
233 | |||
234 | if not no_input and not key.startswith('__'): |
||
235 | val = read_user_dict(key, val) |
||
236 | |||
237 | cookiecutter_dict[key] = val |
||
238 | except UndefinedError as err: |
||
239 | msg = f"Unable to render variable '{key}'" |
||
240 | raise UndefinedVariableInTemplate(msg, err, context) from err |
||
241 | |||
242 | return cookiecutter_dict |
||
243 |