-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgraded_function.py
158 lines (131 loc) · 3.75 KB
/
upgraded_function.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import math
from typing import Literal, Callable, Union
class LazyFunction:
def __init__(self, func):
self._ = self.func_ = func
self.honest_get_attr = make_method("__getattribute__")
self.call2 = make_method(self.func_)
def __getattr__(self, item):
if item[-1] == "_" and (item == "_" or item[-2] != "_"):
return self.__dict__[item]
else:
return self.honest_get_attr(self, item)
def __call__(self, *args):
if any(type(arg) == type(self) for arg in args):
return self.call2(*args)
else:
return self.func_(*args)
methods = [
"__pos__",
"__neg__",
"__invert__",
"__index__",
"__abs__",
"__invert__",
"__round__",
"__getitem__",
"__setitem__",
"__delitem__",
"__cmp__",
"__eq__",
"__ne__",
"__lt__",
"__gt__",
"__le__",
"__ge__",
"__iadd__",
"__isub__",
"__imul__",
"__ifloordiv__",
"__idiv__",
"__itruediv__",
"__imod_",
"__ipow__",
"__ilshift__",
"__irshift__",
"__iand__",
"__ior__",
"__ixor__",
"__enter__",
"__exit__",
"__get__",
"__set__",
"__delete__",
"__copy__",
"__deepcopy__",
"__iter__",
"__next__",
]
method_names = {
"call_": "__call__",
"int_": "__int__",
"bool_": "__bool__",
"str_": "__str__",
"len_": "__len__",
"iter_": "__iter__",
}
functions = {
"not_": lambda a: not a,
"sum_": lambda a: sum(a),
"min_": lambda a: min(a),
"max_": lambda a: max(a),
"list_": lambda a: list(a),
"tuple_": lambda a: tuple(a),
"str_": lambda a: str(a),
"set_": lambda a: set(a),
"any_": lambda a: any(a),
"all_": lambda a: all(a),
"__floor__": lambda a: math.floor(a),
"__ceil__": lambda a: math.ceil(a),
"__trunc__": lambda a: math.trunc(a),
}
rfunctions = {
"__add__": lambda a, b: a + b, # considers "__radd__"
"__sub__": lambda a, b: a - b,
"__mul__": lambda a, b: a * b,
"__floordiv__": lambda a, b: a // b,
"__lshift__": lambda a, b: a << b,
"__rshift__": lambda a, b: a >> b,
"__and__": lambda a, b: a and b,
"__or__": lambda a, b: a or b,
"__pow__": lambda a, b: a ** b,
"__mod__": lambda a, b: a % b,
"__xor__": lambda a, b: a | b,
}
for name, func in rfunctions.items():
def set_func(name, func):
functions[name] = func
functions["__r" + name[2:]] = lambda a, b: func(b, a)
set_func(name, func)
def make_method(method: Union[str, Callable]):
# https://stackoverflow.com/a/19693065
def _method(*args):
def method_func(*func_args):
nonlocal args
args = list(args)
for i, elem in enumerate(args):
if type(elem) == LazyFunction:
args[i] = elem.func_(*func_args)
if type(method) == type(lambda x: 1):
# print(method, *func_args)
return method(*args)
else:
code_str = f"{args[0]}.{method}({args[1:]})"
# print(code_str)
return eval(f"args[0].{method}(*args[1:])")
return LazyFunction(method_func)
return _method
for method_name in methods:
_method = make_method(method_name)
setattr(LazyFunction, method_name, _method)
for method_name, real_method_name in method_names.items():
_method = make_method(real_method_name)
setattr(LazyFunction, method_name, _method)
for method_name, function in functions.items():
_method = make_method(function)
setattr(LazyFunction, method_name, _method)
def lazyfunc_decorator(func):
return LazyFunction(func)
arguments = LazyFunction(lambda *args: args)
arg, arg2, arg3, arg4, arg5 = [arguments[i] for i in range(5)]
arg1 = arg