Slide 12
Slide 12 text
© LayerX Inc. 12
静的解析とASTの例
Python Modelの基礎知識
● Pythonの静的解析には astモジュールが利用可能
● ast.parse(source_code)を使うと、ソースコードが抽象構文木 (AST)に変換され、コードをツリー構造で扱うことができる
import ast
source_code = """
x = 42
def hello():
message = "Hello, dbt!"
print(message)
"""
tree = ast.parse(source_code)
print(ast.dump(tree, indent=4))
Module(
body=[
Assign(targets=[Name(id="x", ctx=Store())], value=Constant(value=42)),
FunctionDef(
name="hello",
args=arguments(
posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]
),
body=[
Assign(
targets=[Name(id="message", ctx=Store())],
value=Constant(value="Hello, dbt!"),
),
Expr(
value=Call(
func=Name(id="print", ctx=Load()),
args=[Name(id="message", ctx=Load())],
keywords=[],
)
),
],
decorator_list=[],
),
],
type_ignores=[])