Skip to main content
Filter by
Sorted by
Tagged with
2 votes
1 answer
101 views

I'm trying to create a function dynamically. Here is an example: import ast import textwrap from typing import Any, Callable, List, Union def create_function( func_name: str, arg_types: List[...
Danila Ganchar's user avatar
0 votes
0 answers
82 views

There are variations, like import ast code = """ import datetime datetime.datetime.now() """ tree = ast.parse(code) print(ast.dump(tree, indent=2)) the ...
phoxd's user avatar
  • 1,652
1 vote
1 answer
135 views

I am currently doing some code manipulation and I am unable to visit an "Assert" statement within a "FunctionDef". How I visit an internal Assert node within a Function node? Here ...
Saurabh Misra's user avatar
1 vote
1 answer
530 views

I'm writing a script that will walk through various Python modules, and hunt for instances of calls to fx.bind(). The first argument to this function is a string representing the hotkey, and the ...
Corin's user avatar
  • 27
1 vote
2 answers
226 views

I have a class (based on this answer) that uses ast.NodeVisitor to get a list of modules imported by a Python file. However, I also want to return the line and column offsets for where the module ...
Stevoisiak's user avatar
  • 27.8k
1 vote
1 answer
258 views

I'm attempting dynamic rewrite of pywin32 win32com.client module prior to import, the below seems to work - but I'm not happy with the 6 lines of code for importing (after the last comment). Can ...
Christopher Horler's user avatar
2 votes
1 answer
582 views

I'm trying to convert every break statement with exec('break') in a code. So far I've got this: import ast source = '''some_list = [2, 3, 4, 5] for i in some_list: if i == 4: p = 0 ...
musava_ribica's user avatar
2 votes
1 answer
518 views

In python 3.8 while doing ast.parse you get a end_lineno variable: import ast code_example = 'from typing import List, Dict' parsed_tree = ast.parse(code_example) for item in parsed_tree.body: ...
ArcLight_Slavik's user avatar
2 votes
1 answer
286 views

The system I work with allows users to specify a Python Boolean expression as a string (in a configuration file). The system takes the string, converts it to a Python AST object and then evaluates ...
Kyle Knoepfel's user avatar
2 votes
1 answer
847 views

I don't pretend to have understood everything in python ast, but FunctionType is something that bothered me. mod = Module(stmt* body, type_ignore *type_ignores) | Interactive(stmt* body) ...
Oussama Hadj Aissa's user avatar
13 votes
2 answers
4k views

How would I generate a .pyc file from a Python AST such that I could import the file from Python? I've used compile to create a code object, then written the co_code attribute to a file, but when I ...
exupero's user avatar
  • 9,476