line_profiler.autoprofile.ast_profle_transformer module

line_profiler.autoprofile.ast_profle_transformer.ast_create_profile_node(modname, profiler_name='profile', attr='add_imported_function_or_module')[source]

Create an abstract syntax tree node that adds an object to the profiler to be profiled.

An abstract syntax tree node is created which calls the attr method from profile and passes modname to it. At runtime, this adds the object to the profiler so it can be profiled. This node must be added after the first instance of modname in the AST and before it is used. The node will look like:

>>> # xdoctest: +SKIP
>>> import foo.bar
>>> profile.add_imported_function_or_module(foo.bar)
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:

expr

AST node that adds modname to profiler.

Return type:

(_ast.Expr)

class line_profiler.autoprofile.ast_profle_transformer.AstProfileTransformer(profile_imports=False, profiled_imports=None, profiler_name='profile')[source]

Bases: NodeTransformer

Transform an abstract syntax tree adding profiling to all of its objects.

Adds profiler decorators on all functions & methods that are not already decorated with the profiler. If profile_imports is True, a profiler method call to profile is added to all imports immediately after the import.

Initializes the AST transformer with the profiler name.

Parameters:
  • profile_imports (bool) – If True, profile all imports.

  • profiled_imports (List[str]) – list of dotted paths of imports to skip that have already been added to profiler.

  • profiler_name (str) – the profiler name used as decorator and for the method call to add to the object to the profiler.

visit_FunctionDef(node)[source]

Decorate functions/methods with profiler.

Checks if the function/method already has a profile_name decorator, if not, it will append profile_name to the end of the node’s decorator list. The decorator is added to the end of the list to avoid conflicts with other decorators e.g. @staticmethod.

Parameters:

(_ast.FunctionDef) – node function/method in the AST

Returns:

node

function/method with profiling decorator

Return type:

(_ast.FunctionDef)

_visit_import(node)[source]

Add a node that profiles an import

If profile_imports is True and the import is not in profiled_imports, a node which calls the profiler method, which adds the object to the profiler, is added immediately after the import.

Parameters:

node (Union[_ast.Import,_ast.ImportFrom]) – import in the AST

Returns:

node
if profile_imports is False:

returns the import node

if profile_imports is True:

returns list containing the import node and the profiling node

Return type:

(Union[Union[_ast.Import,_ast.ImportFrom],List[Union[_ast.Import,_ast.ImportFrom,_ast.Expr]]])

visit_Import(node)[source]

Add a node that profiles an object imported using the “import foo” sytanx

Parameters:

node (_ast.Import) – import in the AST

Returns:

node
if profile_imports is False:

returns the import node

if profile_imports is True:

returns list containing the import node and the profiling node

Return type:

(Union[_ast.Import,List[Union[_ast.Import,_ast.Expr]]])

visit_ImportFrom(node)[source]

Add a node that profiles an object imported using the “from foo import bar” syntax

Parameters:

node (_ast.ImportFrom) – import in the AST

Returns:

node
if profile_imports is False:

returns the import node

if profile_imports is True:

returns list containing the import node and the profiling node

Return type:

(Union[_ast.ImportFrom,List[Union[_ast.ImportFrom,_ast.Expr]]])