1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
from collections.abc import Callable
from typing import Concatenate, Generic, ParamSpec, TypeVar
_P = ParamSpec("_P")
_T = TypeVar("_T")
_O = TypeVar("_O")
_R = TypeVar("_R")
class CruDecorator:
class ImplementedBy(Generic[_O, _P, _R]):
# TODO: Continue here tomorrow!
def __init__(
self, impl: Callable[Concatenate[_O, _P], _R], pass_self: bool
) -> None:
self.impl = impl
self.pass_self = pass_self
def __call__(
self, _origin: Callable[[_T], None]
) -> Callable[Concatenate[_T, _P], _R]:
def real_impl(_self: _T, *args: _P.args, **kwargs: _P.kwargs) -> _R:
if self.pass_self:
return self.impl(_self, *args, **kwargs)
else:
return self.impl(*args, **kwargs)
return real_impl
@staticmethod
def convert_result(
converter: Callable[[_T], _O]
) -> Callable[[Callable[_P, _T]], Callable[_P, _O]]:
def dec(original: Callable[_P, _T]) -> Callable[_P, _O]:
def wrapped(*args: _P.args, **kwargs: _P.kwargs) -> _O:
return converter(original(*args, **kwargs))
return wrapped
return dec
@staticmethod
def create_implement_by(converter: Callable[[_T], _O]) -> Callable[
[Callable[Concatenate[_O, _P], _R]],
Callable[
[Callable[[_T], None]],
Callable[Concatenate[_T, _P], _R],
],
]:
def implement_by(
m: Callable[Concatenate[_O, _P], _R],
) -> Callable[
[Callable[[_T], None]],
Callable[Concatenate[_T, _P], _R],
]:
def implementation(_self: _T, *args: _P.args, **kwargs: _P.kwargs) -> _R:
return m(converter(_self), *args, **kwargs)
def decorator(_: Callable[[_T], None]) -> Callable[Concatenate[_T, _P], _R]:
return implementation
return decorator
return implement_by
@staticmethod
def create_implement_by_no_self() -> Callable[
[Callable[_P, _R]],
Callable[
[Callable[[_T], None]],
Callable[Concatenate[_T, _P], _R],
],
]:
def implement_by_no_self(
m: Callable[_P, _R],
) -> Callable[
[Callable[[_T], None]],
Callable[Concatenate[_T, _P], _R],
]:
def implementation(_self: _T, *args: _P.args, **kwargs: _P.kwargs) -> _R:
return m(*args, **kwargs)
def decorator(_: Callable[[_T], None]) -> Callable[Concatenate[_T, _P], _R]:
return implementation
return decorator
return implement_by_no_self
CRU.add
|