API Reference¶
Every symbol exported from pedros, grouped by area. For behavior details,
edge cases, and runnable examples, follow the link into the Guide.
Decorators¶
timed ¶
A decorator to measure and log the execution time of a function or method. It can be applied to both synchronous and asynchronous functions, and allows customization of the logging level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Any] | None
|
The function to be decorated. If not provided, the decorator can be used with additional configuration through keyword arguments. |
None
|
log_level
|
str | None
|
The logging level to use for reporting execution time. Defaults to "DEBUG". If set to "NONE" (case insensitive), no logging will occur. |
'DEBUG'
|
Returns:
| Type | Description |
|---|---|
Any
|
A decorated function or an asynchronous coroutine that logs its execution time. |
safe ¶
safe(func: Callable[P, Any] | None = None, *, catch: type[Exception] | tuple[type[Exception], ...] = Exception, log_level: str | None = 'ERROR', re_raise: bool = True, on_error: Callable[[Exception], Any] | None = None, on_finally: Callable[[], Any] | None = None) -> Any
A decorator function for safely executing another function within a context where exceptions can be logged, handled, and optionally propagated or suppressed. This utility can also execute additional actions when an error occurs or after the final execution, whether an exception was raised or not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Any] | None
|
The target function to be decorated. If not provided, the decorator is returned to be used with a target function. Defaults to None. |
None
|
catch
|
type[Exception] | tuple[type[Exception], ...]
|
The type of exception or a tuple of exception types to catch. Defaults to Exception. |
Exception
|
log_level
|
str | None
|
The logging level to use when an exception is caught. If set to "NONE", no logging is performed. Defaults to "ERROR". |
'ERROR'
|
re_raise
|
bool
|
Determines whether the caught exceptions should be re-raised after handling. If True, the exception is re-raised. If False, the exception is suppressed. Defaults to True. |
True
|
on_error
|
Callable[[Exception], Any] | None
|
An optional callable to be executed when an exception is caught. The callable should accept the exception instance as its parameter. Defaults to None. |
None
|
on_finally
|
Callable[[], Any] | None
|
An optional callable to be executed in a |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
If |
trace ¶
A decorator to log a function's arguments and return value, or the exception it raised. It never catches or suppresses exceptions, purely observational, and can be applied to both synchronous and asynchronous functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Any] | None
|
The function to be decorated. If not provided, the decorator can be used with additional configuration through keyword arguments. |
None
|
log_level
|
str | None
|
The logging level to use for call/return/error traces. Defaults to "DEBUG". If set to "NONE" (case insensitive), no logging will occur. |
'DEBUG'
|
Returns:
| Type | Description |
|---|---|
Any
|
A decorated function or an asynchronous coroutine that logs its calls. |
monitor ¶
A decorator that combines @trace, @timed, and @safe with their default
settings, with no configuration of its own. Logs the call and return value,
times execution, and catches/logs/re-raises exceptions.
Equivalent to stacking all three with their defaults:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[P, Any]
|
The function to be decorated. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The decorated function, wrapped with tracing, timing, and error handling. |
Logging¶
setup_logging ¶
setup_logging(level: int | str = logging.INFO, logger_name: str = _LIBRARY_LOGGER_NAME, *, add_handler: bool | None = None, propagate: bool | None = None) -> None
Configure logging for one logger without reconfiguring root logging.
This function attempts to use Rich's RichHandler for enhanced,
colorful, and trace-friendly logging. If Rich is not installed,
it silently falls back to Python's standard logging handler.
See more about Rich (https://pypi.org/project/rich/).
Calling this explicitly always takes precedence over pedros's own
auto-configuration (see :func:get_logger), regardless of call order:
it re-derives handler/propagation state from the current root logging
setup every time it runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str
|
Logging level to use as integer or string name. Defaults to |
INFO
|
logger_name
|
str
|
Logger to configure. Defaults to the |
_LIBRARY_LOGGER_NAME
|
add_handler
|
bool | None
|
Whether to attach a handler to |
None
|
propagate
|
bool | None
|
Whether records should propagate to ancestor loggers. By default, propagation is enabled only when root logging is the selected handler path. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
get_logger ¶
Return a logger instance.
If no name is provided, the pedros package logger is returned, and it
is auto-configured on first use if nothing has configured it yet. This is
what powers pedros's own decorators and utilities without requiring
any setup. Auto-configuration is deterministic (own handler, no
propagation) regardless of call order relative to the host application's
logging setup; call :func:setup_logging explicitly to integrate with
root logging instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Name of the logger. If |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
A configured logger instance. |
normalize_log_level ¶
Normalize a log level to a numeric logging value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_level
|
int | str | None
|
A numeric level, a textual level (e.g., |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Numeric level, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided level is unknown. |
Progress bars¶
progbar ¶
progbar(iterable: Iterable[ItemType], *args: Any, backend: Backend | str = 'auto', **kwargs: Any) -> Iterable[ItemType]
Displays a progress bar for the provided iterable, using a chosen backend library.
This function iterates over the elements of the given iterable and optionally displays
a progress bar using either the rich or tqdm libraries. The choice of backend can
be explicitly specified, or automatically determined based on the libraries available
in the environment. If no supported library is found or the backend is set to "none",
no progress bar will be displayed and the original iterable will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
Iterable[ItemType]
|
The iterable whose elements should be iterated over with an optional progress bar. |
required |
args
|
Any
|
Positional arguments to pass to the progress bar library. |
()
|
backend
|
Backend | str
|
The progress bar backend to use. Supported values are:
- "auto" (default): Automatically selects a backend based on available libraries.
- "rich": Uses the |
'auto'
|
kwargs
|
Any
|
Additional keyword arguments to customize the behavior of the progress bar. These
are dependent on the backend used:
- For |
{}
|
Returns:
| Type | Description |
|---|---|
Iterable[ItemType]
|
The input iterable, optionally wrapped with a progress bar based on the selected backend. |
Dependency checks¶
has_dep ¶
Check if a specified dependency is available on the system.
This function verifies the availability of a Python module or package by checking if it can be located using the Python runtime's import system. When a version constraint is provided, the dependency must also satisfy the constraint using installed distribution metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the dependency to check. |
required |
version
|
str | None
|
Optional exact version or comma-separated range constraint. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the dependency is available, False otherwise. |