line_profiler.autoprofile.profmod_extractor module

class line_profiler.autoprofile.profmod_extractor.ProfmodExtractor(tree, script_file, prof_mod)[source]

Bases: object

Map prof_mod to imports in an abstract syntax tree.

Takes the paths and dotted paths in prod_mod and finds their respective imports in an abstract syntax tree.

Initializes the AST tree profiler instance with the AST, script file path and prof_mod

Parameters:
  • tree (_ast.Module) – abstract syntax tree to fetch imports from.

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

static _is_path(text)[source]

Check whether a string is a path.

Checks if a string contains a slash or ends with .py indicating it is a path.

Parameters:

text (str) – string to check whether it is a path or not

Returns:

bool indicating whether the string is a path or not

Return type:

ret (bool)

classmethod _get_modnames_to_profile_from_prof_mod(script_file, prof_mod)[source]

Grab the valid paths and all dotted paths in prof_mod and their subpackages and submodules, in the form of dotted paths.

First all items in prof_mod are converted to a valid path. if unable to convert, check if the item is an invalid path and skip it, else assume it is an installed package. The valid paths are then converted to dotted paths. The converted dotted paths along with the items assumed to be installed packages are added a list of modnames_to_profile. Then all subpackages and submodules under each valid path is fetched, converted to dotted path and also added to the list. if script_file is in prof_mod it is skipped to avoid name collision with othe imports, it will be processed elsewhere in the autoprofile pipeline.

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:

list of dotted paths to profile.

Return type:

modnames_to_profile (List[str])

static _ast_get_imports_from_tree(tree)[source]

Get all imports in an abstract syntax tree.

Parameters:

tree (_ast.Module) – abstract syntax tree to fetch imports from.

Returns:

list of dicts of all imports in the tree, containing:
name (str):

the real name of the import. e.g. foo from “import foo as bar”

alias (str):

the alias of an import if applicable. e.g. bar from “import foo as bar”

tree_index (int):

the index of the import as found in the tree

Return type:

module_dict_list (List[Dict[str,Union[str,int]]])

static _find_modnames_in_tree_imports(modnames_to_profile, module_dict_list)[source]

Map modnames to imports from an abstract sytax tree.

Find imports in modue_dict_list, created from an abstract syntax tree, that match dotted paths in modnames_to_profile. When a submodule is imported, both the submodule and the parent module are checked whether they are in modnames_to_profile. As the user can ask to profile “foo” when only “from foo import bar” is imported, so both foo and bar are checked. The real import name of an import is used to map to the dotted paths. The import’s alias is stored in the output dict.

Parameters:
  • modnames_to_profile (List[str]) – list of dotted paths to profile.

  • module_dict_list (List[Dict[str,Union[str,int]]]) – list of dicts of all imports in the tree.

Returns:

dict of imports found
key (int):

index of import in AST

value (str):

alias (or name if no alias used) of import

Return type:

modnames_found_in_tree (Dict[int,str])

run()[source]

Map prof_mod to imports in an abstract syntax tree.

Takes the paths and dotted paths in prod_mod and finds their respective imports in an abstract syntax tree, returning their alias and the index they appear in the AST.

Returns:

tree_imports_to_profile_dict
dict of imports to profile
key (int):

index of import in AST

value (str):

alias (or name if no alias used) of import

Return type:

(Dict[int,str])