| Total Complexity | 9 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Coverage | 82.61% |
| Changes | 0 | ||
| 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 |