abstract-syntax-tree

error in counting code lines of a function after parsing a .py file using Python’s ast library

error in counting code lines of a function after parsing a .py file using Python’s ast library Question: the line count after ast processing is incomplete, with only the first two being complete and everything after ‘draw questions’ being incomplete. lineCount.py is here import ast def get_function_info(file_path): with open(file_path, ‘r’) as f: tree = ast.parse(f.read()) …

Total answers: 1

Pythons inspect.getsource throws error if used in a decorator

Pythons inspect.getsource throws error if used in a decorator Question: I have the following function def foo(): for _ in range(1): print("hello") Now I want to add another print statement to print "Loop iterated" after every loop iteration. For this I define a new function that transforms foo into an ast tree, inserts the corresponding …

Total answers: 1

How to parse custom operators inside a evaluable python string?

How to parse custom operators inside a evaluable python string? Question: Having a formula as a string like: str_forumla = "x > 0 AND y < 5 AND ‘this AND that’ in my_string" Where the python operator "&" have been substituted by "AND" (but the string AND was not affected) how to revert the operation …

Total answers: 2

Edit attribute in script string with AST

Edit attribute in script string with AST Question: I’m unfamiliar with the AST module and would appreciate any insight. If, for example, I have a string that contains a valid python script such as import sys #Just any module class SomeClass: def __init__(self): self.x = 10 self.b = 15 def a_func(self): print(self.x) I would like …

Total answers: 1

Is there abstract syntax tree (AST) in python extension module (files with suffix .so)?

Is there abstract syntax tree (AST) in python extension module (files with suffix .so)? Question: I can check AST in python file: python3 -m ast some_file.py But, when I compile it with nuitka: nuitka3 –module some_file.py I get some_file.so extension module and when I run python3 -m ast some_file.so I get error. So, question my …

Total answers: 1

Python AST : Insert argument to class

Python AST : Insert argument to class Question: Using AST to add argument to the existing class. In the AST world, it is classdef which I already found, how do I add an argument to that classDef or update the bases basically ? From class testApp: To class testApp(Core): Asked By: shailesh gavathe || Source …

Total answers: 1

Python ast Libary – retreive value of Node inside a function call

Python ast Libary – retreive value of Node inside a function call Question: Follow up question of here: Python ast Libary – how to retreive value of a specific node I use the following code to retreive {‘console_scripts’: [‘main=smamesdemo.run.main:main’]} import ast code = ”’extras_require={"dev": dev_reqs} x = 3 entry_points={ "console_scripts": ["main=smamesdemo.run.main:main"] }”’ for node in …

Total answers: 1

Python ast Libary – how to retreive value of a specific node

Python ast Libary – how to retreive value of a specific node Question: I have the following script: extras_require={"dev": dev_reqs} x = 3 entry_points={ "console_scripts": ["main=smamesdemo.run.main:main"] } I want to retrieve the following part as python data, dict and list not Nodes. entry_points={ "console_scripts": ["main=smamesdemo.run.main:main"] } I have tried the following, but I am not …

Total answers: 2