Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

coalib.collecting.FunctionMetadata.from_function()   B

Complexity

Conditions 6

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 6
dl 0
loc 54
rs 7.852

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
class CircularDependencyError(Exception):
2
    @classmethod
3
    def for_bears(cls, bears):
4
        """
5
        Creates the CircularDependencyError with a helpful message about the
6
        dependency.
7
        """
8
        bear_names = []
9
10
        for bear in bears:
11
            bear_names.append(bear.__name__)
12
13
        return cls("Circular dependency detected: " + " -> ".join(bear_names))
14
15
16
def _resolve(bears, resolved_bears, seen):
17
    for bear in bears:
18
        if bear in resolved_bears:
19
            continue
20
21
        missing = bear.missing_dependencies(resolved_bears)
22
        if missing == []:
23
            resolved_bears.append(bear)
24
            continue
25
26
        if bear in seen:
27
            seen.append(bear)
28
            raise CircularDependencyError.for_bears(seen)
29
30
        seen.append(bear)
31
        resolved_bears = _resolve(missing, resolved_bears, seen)
32
        resolved_bears.append(bear)
33
        seen.remove(bear)  # Already resolved, no candidate for circular dep
34
35
    return resolved_bears
36
37
38
def resolve(bears):
39
    """
40
    Collects all dependencies of the given bears. This will also remove
41
    duplicates.
42
43
    :param bears: The given bears. Will not be modified.
44
    :return:      The new list of bears, sorted so that it can be executed
45
                  sequentially without dependency issues.
46
    """
47
    return _resolve(bears, [], [])
48