lagom.util.functional   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 9
eloc 24
dl 0
loc 52
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A FunctionCollection.__iter__() 0 2 1
A FunctionCollection.__init__() 0 3 1
A FunctionCollection.__eq__() 0 6 3
A FunctionCollection.__len__() 0 2 1
A FunctionCollection.__contains__() 0 2 1
A FunctionCollection.__hash__() 0 2 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A arity() 0 12 1
1
"""Code to help understand functions
2
"""
3
4 1
import inspect
5 1
from typing import Callable, TypeVar, Generic, Iterator
6
7
8 1
def arity(func: Callable) -> int:
9
    """Returns the arity(number of args)
10
    Given a callable this function returns the number of arguments it expects
11
    >>> arity(lambda: 5)
12
    0
13
    >>> arity(lambda x: x)
14
    1
15
16
    :param func:
17
    :return:
18
    """
19 1
    return len(inspect.signature(func).parameters)
20
21
22 1
F = TypeVar("F", bound=Callable)
23
24
25 1
class FunctionCollection(Generic[F]):
26
    """
27
    Represents a collection of functions that is hashable.
28
    """
29
30 1
    def __init__(self, *checkers: F):
31 1
        self._checkers = checkers
32 1
        self.hash = hash(tuple(self._checkers))
33
34 1
    def __len__(self) -> int:
35
        return len(self._checkers)
36
37 1
    def __contains__(self, item) -> bool:
38
        return item in self._checkers
39
40 1
    def __iter__(self) -> Iterator[F]:
41 1
        return iter(self._checkers)
42
43 1
    def __hash__(self):
44
        return self.hash
45
46 1
    def __eq__(self, other):
47 1
        if isinstance(other, FunctionCollection):
48 1
            return self.hash == other.hash
49 1
        if isinstance(other, list):
50 1
            return tuple(other) == self._checkers
51
        return False
52