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