-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun.py
72 lines (51 loc) · 1.61 KB
/
run.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
"""
Functions to read data from a python function.
"""
import ast
import pandas as pd
from dagster import get_dagster_logger
def validate_function_definition(code_str: str, function_name: str) -> bool:
"""
Check if the code_str contains a function definition with the given name.
Args:
code_str (str): The code string to parse.
function_name (str): The name of the function to search for.
Returns:
bool: True if the function definition is found, False otherwise.
"""
try:
parsed_ast = ast.parse(code_str)
for node in parsed_ast.body:
if isinstance(node, ast.FunctionDef) and node.name == function_name:
return True
return False
except SyntaxError:
return False
def define_fn(fn_name: str, fn: str) -> str:
"""
Define a python function.
Args:
fn_name (str): The name of the function to define.
fn (str): The code string representing the function.
Returns:
str: The name of the defined function.
"""
logger = get_dagster_logger()
logger.debug(f"fn_name: {fn_name}")
logger.debug(f"fn: {fn}")
namespace = {}
exec(fn, globals(), namespace)
return namespace[fn_name]
def run_df_fn(fn_name: str, fn: str) -> pd.DataFrame:
"""
Run a python function.
Args:
fn_name (str): The name of the function to run.
fn (str): The code string representing the function.
Returns:
pd.DataFrame: The result of running the function.
"""
# fn_name = define_fn(fn_name, fn)
exec(fn)
df = locals()[fn_name]()
return df