line_profiler.cli_utils module¶
Shared utilities between the python -m line_profiler and kernprof CLI tools.
- line_profiler.cli_utils.add_argument(parser_like, arg: str, /, *args: str, hide_complementary_options: bool = True, **kwargs: object) Action[source]¶
Override the
'store_true'and'store_false'actions so that they are turned into options which:Don’t set the default to the opposite boolean, thus allowing us to later distinguish between cases where the flag has been passed or not, and
Set the destination value to the corresponding value in the no-arg form, but also allow (for long options) for a single arg which is parsed by
boolean().
Also automatically generates complementary boolean options for
action='store_true'options. Ifhide_complementary_optionsis true, the auto-generated option (all the long flags prefixed with'no-', e.g.'--foo'is negated by'--no-foo') is hidden from the help text.- Parameters:
parser_like (Any) – Object having a method
add_argument(), which has the same semantics and call signature asargparse.ArgumentParser.add_argument().hide_complementary_options (bool) – Whether to hide the auto-generated complementary options to
action='store_true'options from the help text for brevity.arg, *args, **kwargs – Passed to
parser_like.add_argument()
- Returns:
- action_like
Return value of
parser_like.add_argument()
- Return type:
Any
Note
Short and long flags for
'store_true'and'store_false'actions are implemented in separate actions so as to allow for short-flag concatenation.If an option has both short and long flags, the short-flag action is hidden from the help text, but the long-flag action’s help text is updated to mention the corresponding short flag(s).
- line_profiler.cli_utils.get_cli_config(subtable: str, /, config: str | PathLike[str] | bool | None = None, *, read_env: bool = True) ConfigSource[source]¶
Get the
tool.line_profiler.<subtable>configs and normalize its keys (some-key->some_key).- Parameters:
subtable (str) – Name of the subtable the CLI app should refer to (e.g.
'kernprof')*args, **kwargs – Passed to
line_profiler.toml_config.ConfigSource.from_config()
- Returns:
New
ConfigSourceinstance
- line_profiler.cli_utils.get_python_executable() str[source]¶
- Returns:
- command
Command or path thereto corresponding to
sys.executable.
- Return type:
- line_profiler.cli_utils.positive_float(value: str) float[source]¶
- Parameters:
value (str)
- Returns:
positive_num
- Return type:
- line_profiler.cli_utils.boolean(value: str, *, fallback: bool | None = None, invert: bool = False) bool[source]¶
- Parameters:
value (str) – Value to be parsed into a boolean (case insensitive)
fallback (bool | None) – Optional value to fall back to in case
valuedoesn’t match any of the specifiedinvert (bool) – If
True, invert the result of parsingvalue(but notfallback)
- Returns:
result
- Return type:
Example
These values are parsed into
False:>>> assert not any( ... boolean(value) ... for value in ['', '0', 'F', 'N', 'off', 'False', 'no'])
These values are parsed into
True:>>> assert all( ... boolean(value) ... for value in ['1', 'T', 'Y', 'on', 'True', 'yes'])
Fallback:
>>> assert boolean('invalid', fallback=True) == True >>> assert boolean('invalid', fallback=False) == False >>> try: ... result = boolean('invalid') ... except ValueError: ... pass ... except Exception as e: ... assert False, ( ... f'Expected `ValueError`, got `{type(e).__name__}`') ... else: ... assert False, ( ... f'Expected `ValueError`, got result {result!r}')
Case insensitivity:
>>> assert boolean('fAlSe') == False >>> assert boolean('YeS') == True
- line_profiler.cli_utils.short_string_path(path: str | PathLike[str]) str[source]¶
- Parameters:
path (str | os.PathLike[str]) – Path-like
- Returns:
- short_path
The shortest formatted path among the provided
path, the corresponding absolute path, and its relative path to the current directory.
- Return type: