line_profiler.autoprofile.ast_tree_profiler module

class line_profiler.autoprofile.ast_tree_profiler.AstTreeProfiler(script_file, prof_mod, profile_imports, ast_transformer_class_handler=<class 'line_profiler.autoprofile.ast_profle_transformer.AstProfileTransformer'>, profmod_extractor_class_handler=<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.

static _check_profile_full_script(script_file, prof_mod)[source]

Check whether whole script should be profiled.

Checks whether path to script has been passed to prof_mod indicating that the whole script should be profiled

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).

Returns:

profile_full_script

if True, profile whole script.

Return type:

(bool)

static _get_script_ast_tree(script_file)[source]

Generate an abstract syntax from a script file.

Parameters:

script_file (str) – path to script being profiled.

Returns:

abstract syntax tree of the script.

Return type:

tree (_ast.Module)

_profile_ast_tree(tree, tree_imports_to_profile_dict, profile_full_script=False, profile_imports=False)[source]

Add profiling to an abstract syntax tree.

Adds nodes to the AST that adds the specified objects to the profiler. If profile_full_script is True, all functions/methods, classes & modules in the script have a node added to the AST to add them to the profiler. If profile_imports is True as well as profile_full_script, all imports are have a node added to the AST to add them to the profiler.

Parameters:
  • tree (_ast.Module) – abstract syntax tree to be profiled.

  • tree_imports_to_profile_dict (Dict[int,str]) –

    dict of imports to profile
    key (int):

    index of import in AST

    value (str):

    alias (or name if no alias used) of import

  • profile_full_script (bool) – if True, profile whole script.

  • profile_imports (bool) – if True, and profile_full_script is True, profile all imports aswell.

Returns:

tree

abstract syntax tree with profiling.

Return type:

(_ast.Module)

profile()[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)