1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to extract arguments from call with ast python

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024 às 13:12.

  1. Stack

    Stack Membro Participativo

    I am working on a function to convert function calls, therefor i need the calls and parameter. I fiddled with ast and i am able to extract the specific nodes, but somehow i cant distinguish the node by their type. For example, i want to get the call and the parameter to do my convertion in a new syntax Example code:

    import ast import argparse

    def __init__(self):
    self.arglist = []
    def visit(self, node):
    if isinstance(node, ast.Call):
    print('VVVVVVVVVVVVVV CALL ANFANG VVVVVVVVVVVVVVVV')
    print(f'_______ Call found ______')
    super().visit(node)
    print(f'Argumente: >>>{node.args.pop(0).__dict__}<<<<')
    print('______________ CALL END ________________')
    if isinstance(node, ast.Name):
    print(f'_______ Name found ______ {node.__class__}')
    print(f'Name Argumente: {node.id.__str__()}')
    return super().visit(node)
    elif isinstance(node, ast.For):
    print(f'_______ FOR found ______ ')
    return super().visit(node)
    elif isinstance(node, ast.keyword):
    print(f'_______ keyword found ______ {node.__class__}')
    return super().visit(node)
    elif isinstance(node, ast.Attribute):
    return super().visit(node)
    print(f'_______ Attribute fopund _____ {node.__class__}')
    elif isinstance(node, ast.Expr):
    return super().visit(node)
    print(f'_______ Expr found ________ {node.__class__}')
    elif isinstance(node, ast.Constant):
    print(f'_______ Konstante found!!>>>> {node.value}')
    return super().visit(node)

    return super().visit(node)



    tree = ast.parse('''
    meat = get_mess(ONE, TWO, THREE)
    name2 = 'Elise'
    ''')

    print(ast.dump(tree))
    vis = Visitor()
    vis.visit(tree)```

    Output is:

    Module(body=[Assign(targets=[Name(id='meat', ctx=Store())], value=Call(func=Name(id='get_mess', ctx=Load()), args=[Name(id='ONE', ctx=Load()), Name(id='TWO', ctx=Load()), Name(id='THREE', ctx=Load())], keywords=[])), Assign(targets=[Name(id='name2', ctx=Store())], value=Constant(value='Elise'))], type_ignores=[])
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: meat
    VVVVVVVVVVVVVV CALL ANFANG VVVVVVVVVVVVVVVV
    _______ Call found ______
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: get_mess
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: ONE
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: TWO
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: THREE
    Argumente: >>>{'id': 'ONE', 'ctx': <ast.Load object at 0x7f42d71fca10>, 'lineno': 2, 'col_offset': 16, 'end_lineno': 2, 'end_col_offset': 19}<<<<
    ______________ CALL END ________________
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: get_mess
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: TWO
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: THREE
    _______ Name found ______ <class 'ast.Name'>
    Name Argumente: name2
    _______ Konstante found!!>>>> Elise


    It is extracting function calls (get_mess) and parameters (ONE, TWO, THREE) but i didnt found a way to extract them by their type so i can build new code.

    Continue reading...

Compartilhe esta Página