-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecute_python.py
More file actions
71 lines (58 loc) · 1.87 KB
/
execute_python.py
File metadata and controls
71 lines (58 loc) · 1.87 KB
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
class AnyType(str):
def __ne__(self, __value: object) -> bool:
return False
ANY = AnyType("*")
class ByPassTypeTuple(tuple):
"""
Inspired by rgthree-comfy
https://github.com/rgthree/rgthree-comfy
"""
def __len__(self):
return 10
def __getitem__(self, index):
return super().__getitem__(0)
class ExecutePython:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"code": ("STRING", {"multiline": True, "dynamicPrompts": False}),
"n_outputs": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}),
},
"optional": {
"arg0": (ANY,),
},
}
RETURN_TYPES = ByPassTypeTuple((ANY,))
RETURN_NAMES = ByPassTypeTuple(("res0",))
FUNCTION = "execute_code"
CATEGORY = "utils"
def execute_code(self, code, n_outputs, **kwargs):
context = {}
context.update(kwargs)
try:
exec(code, context)
except Exception as e:
raise e
if "result" in context:
result = context["result"]
if not isinstance(result, tuple):
if n_outputs == 1:
result = (result,)
elif isinstance(result, list):
result = tuple(result)
else:
raise ValueError(
f"[ExecutePython] expected a tuple, got {type(result)}"
)
if len(result) != n_outputs:
raise ValueError(
f"[ExecutePython] expected tuple of length {n_outputs}, got {len(result)}"
)
return result
else:
raise RuntimeError(
"[ExecutePython] `result` variable was not assigned in the Python code"
)