Conditions | 8 |
Total Lines | 51 |
Lines | 0 |
Ratio | 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:
1 | # -*- coding: utf-8 -*- |
||
11 | def mktree(crumb, values_map): |
||
12 | """ Create the tree of folders given the values for the crumb arguments |
||
13 | of the current crumb path. |
||
14 | Parameters |
||
15 | ---------- |
||
16 | crumb: Crumb |
||
17 | |||
18 | values_map: Sequence[Sequence[2-Tuple[str, str]]] or Sequence[Dict[str, str]] |
||
19 | The lists of values to substitute each crumb argument that you want. |
||
20 | Do not leave dependent arguments alone. |
||
21 | Example: [[('subject_id', 'pat_0001'), ('session_id', 'session_1')], |
||
22 | [('subject_id', 'pat_0002'), ('session_id', 'session_1')], |
||
23 | .... |
||
24 | ] |
||
25 | |||
26 | Example: [{'subject_id': 'pat_0001', 'session_id': 'session_1'}, |
||
27 | {'subject_id': 'pat_0002', 'session_id': 'session_1'}, |
||
28 | .... |
||
29 | ] |
||
30 | |||
31 | Returns |
||
32 | ------- |
||
33 | paths: list of Paths |
||
34 | The paths that have been created. |
||
35 | """ |
||
36 | if values_map is None: |
||
37 | return [crumb.touch()] |
||
38 | |||
39 | if not isinstance(values_map, (list, dict)): |
||
40 | raise TypeError("Expected keys in `values_map` to be a Sequence, " |
||
41 | "got {}.".format(type(values_map))) |
||
42 | |||
43 | paths = [] |
||
44 | for idx, aval in enumerate(values_map): |
||
45 | if not isinstance(aval, Mapping): |
||
46 | aval = dict(aval) |
||
47 | |||
48 | if not set(aval.keys()).issubset(set(crumb.all_args())): |
||
49 | raise ValueError("Expected keys in `values_map` item to be a subset of {}, " |
||
50 | "got {}.".format(crumb.all_args(), aval.keys())) |
||
51 | |||
52 | rem_deps = crumb._args_open_parents(list(aval.keys())) |
||
53 | if rem_deps: |
||
54 | raise KeyError("Expected `values_map` item to not leave crumbs alone," |
||
55 | " you forgot to add: {} in item {}".format(rem_deps, idx)) |
||
56 | |||
57 | paths.append(crumb.replace(**aval)) |
||
58 | |||
59 | _ = [path.touch() for path in paths] |
||
60 | |||
61 | return paths |
||
62 |