-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
140 lines (113 loc) · 3.51 KB
/
__init__.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import inspect
from copy import deepcopy
def copy_func(f):
if callable(f):
if inspect.ismethod(f) or inspect.isfunction(f):
g = lambda *args, **kwargs: f(*args, **kwargs)
t = list(filter(lambda prop: not ("__" in prop), dir(f)))
i = 0
while i < len(t):
setattr(g, t[i], getattr(f, t[i]))
i += 1
return g
dcoi = deepcopy([f])
return dcoi[0]
class FlexiblePartial:
def __init__(self, func, this_args_first=True, *args, **kwargs):
self.this_args_first = this_args_first
try:
self.modulename = func.__module__
except Exception:
self.modulename = ""
try:
self.functionname = func.__name__
except Exception:
try:
self.functionname = func.__qualname__
except Exception:
self.functionname = "func"
try:
self.f = copy_func(func)
except Exception:
self.f = func
try:
self.args = copy_func(list(args))
except Exception:
self.args = args
try:
self.kwargs = copy_func(kwargs)
except Exception:
try:
self.kwargs = kwargs.copy()
except Exception:
self.kwargs = kwargs
self.name_to_print = self._create_name()
def _create_name(self):
if self.modulename != "":
stra = self.modulename + "." + self.functionname + "("
else:
stra = self.functionname + "("
for _ in self.args:
stra = stra + repr(_) + ", "
for key, item in self.kwargs.items():
stra = stra + str(key) + "=" + repr(item) + ", "
stra = stra.rstrip().rstrip(",")
stra += ")"
if len(stra) > 100:
stra = stra[:95] + "...)"
return stra
def __call__(self, *args, **kwargs):
newdic = {}
newdic.update(self.kwargs)
newdic.update(kwargs)
if self.this_args_first:
return self.f(*self.args, *args, **newdic)
else:
return self.f(*args, *self.args, **newdic)
def __str__(self):
return self.name_to_print
def __repr__(self):
return self.__str__()
class FlexiblePartialOwnName:
r"""
FlexiblePartial(
remove_file,
"()",
True,
fullpath_on_device=x.aa_fullpath,
adb_path=adb_path,
serialnumber=device,
)
"""
def __init__(
self, func, funcname: str, this_args_first: bool = True, *args, **kwargs
):
self.this_args_first = this_args_first
self.funcname = funcname
try:
self.f = copy_func(func)
except Exception:
self.f = func
try:
self.args = copy_func(list(args))
except Exception:
self.args = args
try:
self.kwargs = copy_func(kwargs)
except Exception:
try:
self.kwargs = kwargs.copy()
except Exception:
self.kwargs = kwargs
def __call__(self, *args, **kwargs):
newdic = {}
newdic.update(self.kwargs)
newdic.update(kwargs)
if self.this_args_first:
return self.f(*self.args, *args, **newdic)
else:
return self.f(*args, *self.args, **newdic)
def __str__(self):
return self.funcname
def __repr__(self):
return self.funcname