line_profiler.autoprofile.run_module module¶
- line_profiler.autoprofile.run_module.get_module_from_importfrom(node: ImportFrom, module: str) str[source]¶
Resolve the full path of a relative import.
- Parameters:
node (ast.ImportFrom) – ImportFrom node
module (str) – Full dotted path relative to which the import is to occur
- Returns:
- modname (str)
Full path of the module from which the names are to be imported
Example
>>> import ast >>> import functools >>> import textwrap >>> >>> >>> abs_import, *rel_imports = ast.parse(textwrap.dedent(''' ... from a import b ... from . import b ... from .. import b ... from .baz import b ... from ..baz import b ... '''.strip('\n'))).body >>> >>> >>> get_module = functools.partial( ... get_module_from_importfrom, module='foo.bar.foobar') >>> assert get_module(abs_import) == 'a' >>> assert get_module(rel_imports[0]) == 'foo.bar' >>> assert get_module(rel_imports[1]) == 'foo' >>> assert get_module(rel_imports[2]) == 'foo.bar.baz' >>> assert get_module(rel_imports[3]) == 'foo.baz'
- class line_profiler.autoprofile.run_module.ImportFromTransformer(module: str)[source]¶
Bases:
NodeTransformerTurn all the relative imports into absolute imports.
- visit_ImportFrom(node: ImportFrom) ImportFrom[source]¶
- class line_profiler.autoprofile.run_module.AstTreeModuleProfiler(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:
AstTreeProfilerCreate an abstract syntax tree of an executable module and add profiling to it.
Reads the module code 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.