line_profiler.scoping_policy module¶
- line_profiler.scoping_policy.DEFAULT_SCOPING_POLICIES: ScopingPolicyDict = {'class': 'siblings', 'func': 'siblings', 'module': 'exact'}¶
Default scoping policies:
Profile sibling and descendant functions (
ScopingPolicy.SIBLINGS)Descend ingo sibling and descendant classes (
ScopingPolicy.SIBLINGS)Don’t descend into modules (
ScopingPolicy.EXACT)
- class line_profiler.scoping_policy.ScopingPolicy(*values)[source]¶
Bases:
StringEnumStrEnumfor scoping policies, that is, how it is decided whether to:Profile a function found in a namespace (a class or a module), and
Descend into nested namespaces so that their methods and functions are profiled,
when using
LineProfiler.add_class(),LineProfiler.add_module(), andadd_imported_function_or_module().Available policies are:
ScopingPolicy.EXACTOnly profile functions found in the namespace fulfilling
ScopingPolicy.CHILDRENas defined below, without descending into nested namespacesScopingPolicy.CHILDRENOnly profile/descend into child objects, which are:
Classes and functions defined locally in the very module, or in the very class as its “inner classes” and methods
Direct submodules, in case when the namespace is a module object representing a package
ScopingPolicy.DESCENDANTSOnly profile/descend into descendant objects, which are:
Child classes, functions, and modules, as defined above in
ScopingPolicy.CHILDRENTheir child classes, functions, and modules, …
… and so on
- Note:
Since imported submodule module objects are by default placed into the namespace of their parent-package module objects, this functions largely identical to
ScopingPolicy.CHILDRENfor descent from module objects into other modules objects.
ScopingPolicy.SIBLINGSOnly profile/descend into sibling and descendant objects, which are:
Descendant classes, functions, and modules, as defined above in
ScopingPolicy.DESCENDANTSClasses and functions (and descendants thereof) defined in the same parent namespace to this very class, or in modules (and subpackages and their descendants) sharing a parent package to this very module
Modules (and subpackages and their descendants) sharing a parent package, when the namespace is a module
ScopingPolicy.NONEDon’t check scopes; profile all functions found in the local namespace of the class/module, and descend into all nested namespaces recursively
- Note:
This is probably a very bad idea for module scoping, potentially resulting in accidentally recursing through a significant portion of loaded modules; proceed with care.
Note
Other than
enum.Enummethods starting and ending with single underscores (e.g._missing_()), all methods prefixed with a single underscore are to be considered implementation details.- EXACT = 'exact'¶
- CHILDREN = 'children'¶
- DESCENDANTS = 'descendants'¶
- SIBLINGS = 'siblings'¶
- NONE = 'none'¶
- get_filter(namespace: type | ModuleType, obj_type: Literal['func']) Callable[[Callable], bool][source]¶
- get_filter(namespace: type | ModuleType, obj_type: Literal['class']) Callable[[type], bool]
- get_filter(namespace: type | ModuleType, obj_type: Literal['module']) Callable[[ModuleType], bool]
- Parameters:
namespace (Union[type, types.ModuleType]) – Class or module to be profiled.
obj_type (Literal[‘func’, ‘class’, ‘module’]) – Type of object encountered in
namespace:'func'Either a function, or a component function of a callable-like object (e.g.
property)'class'(resp.'module')A class (resp. a module)
- Returns:
Filter callable returning whether the argument (as specified by
obj_type) should be added viaLineProfiler.add_class(),LineProfiler.add_module(), orLineProfiler.add_callable()- Return type:
- classmethod to_policies(policies: str | ScopingPolicy | ScopingPolicyDict | None = None) _ScopingPolicyDict[source]¶
Normalize
policiesinto a dictionary of policies for various object types.- Parameters:
policies (Union[str, ScopingPolicy, ScopingPolicyDict, None]) –
ScopingPolicy, string convertible thereto (case-insensitive), or a mapping containing such values and the keys as outlined in the return value; the defaultNoneis equivalent toDEFAULT_SCOPING_POLICIES.- Returns:
Dictionary with the following key-value pairs:
'func'ScopingPolicyfor profiling functions and other callable-like objects composed thereof (e.g.property).'class'ScopingPolicyfor descending into classes.'module'ScopingPolicyfor descending into modules (if the namespace is itself a module).
- Return type:
normalized_policies (dict[Literal[‘func’, ‘class’, ‘module’], ScopingPolicy])
Note
If
policiesis a mapping, it is required to contain all three of the aforementioned keys.Example
>>> assert (ScopingPolicy.to_policies('children') ... == dict.fromkeys(['func', 'class', 'module'], ... ScopingPolicy.CHILDREN)) >>> assert (ScopingPolicy.to_policies({ ... 'func': 'NONE', ... 'class': 'descendants', ... 'module': 'exact', ... 'unused key': 'unused value'}) ... == {'func': ScopingPolicy.NONE, ... 'class': ScopingPolicy.DESCENDANTS, ... 'module': ScopingPolicy.EXACT}) >>> ScopingPolicy.to_policies({}) Traceback (most recent call last): ... KeyError: 'func'
- class line_profiler.scoping_policy.ScopingPolicyDict¶
Bases:
TypedDict- func: str | ScopingPolicy¶
- class: str | ScopingPolicy¶
- module: str | ScopingPolicy¶