line_profiler.autoprofile.ast_tree_profiler module

class line_profiler.autoprofile.ast_tree_profiler.AstTreeProfiler(script_file: str, prof_mod: list[str], profile_imports: bool, ast_transformer_class_handler: ~typing.Type = <class 'line_profiler.autoprofile.ast_profile_transformer.AstProfileTransformer'>, profmod_extractor_class_handler: ~typing.Type = <class 'line_profiler.autoprofile.profmod_extractor.ProfmodExtractor'>)[source]

Bases: object

Create an abstract syntax tree of a script and add profiling to it.

Reads a script file and generates an abstract syntax tree, then adds nodes and/or decorators to the AST that adds the specified functions/methods, classes & modules in prof_mod to the profiler to be profiled.

Initializes the AST tree profiler instance with the script file path

Parameters:
  • script_file (str) – path to script being profiled.

  • prof_mod (List[str]) – list of imports to profile in script. passing the path to script will profile the whole script. the objects can be specified using its dotted path or full path (if applicable).

  • profile_imports (bool) – if True, when auto-profiling whole script, profile all imports aswell.

  • ast_transformer_class_handler (Type) – the AstProfileTransformer class that handles profiling the whole script.

  • profmod_extractor_class_handler (Type) – the ProfmodExtractor class that handles mapping prof_mod to objects in the script.

profile() Module[source]

Create an abstract syntax tree of a script and add profiling to it.

Reads a script file and generates an abstract syntax tree. Then matches imports in the script’s AST with the names in prof_mod. The matched imports are added to the profiler for profiling. If path to script is found in prof_mod, all functions/methods, classes & modules are added to the profiler. If profile_imports is True as well as path to script in prof_mod, all the imports in the script are added to the profiler.

Returns:

tree

abstract syntax tree with profiling.

Return type:

(_ast.Module)