Completed
Push — master ( da0b24...226022 )
by Alexandre M.
01:04
created

mktree()   C

Complexity

Conditions 8

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 8
dl 0
loc 51
rs 5.2591

How to fix   Long Method   

Long Method

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:

1
# -*- coding: utf-8 -*-
2
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3
# vi: set ft=python sts=4 ts=4 sw=4 et:
4
"""
5
Crumb class: the smart path model class.
6
"""
7
8
from collections import Mapping
9
10
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