Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve progress bar update API #681

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/lenskit/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from lenskit import __version__
from lenskit.logging import LoggingConfig, console, get_logger
from lenskit.logging.processors import error_was_logged

__all__ = ["lenskit", "main", "version"]
_log = get_logger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions src/lenskit/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import structlog

from ._console import ConsoleHandler, console, setup_console
from .processors import format_timestamp, log_warning, record_logged_error, remove_internal
from .processors import format_timestamp, log_warning, remove_internal
from .progress import set_progress_impl
from .tracing import lenskit_filtering_logger

Expand Down Expand Up @@ -187,7 +187,6 @@ def apply(self):
)
formatter = structlog.stdlib.ProcessorFormatter(
processors=[
record_logged_error,
remove_internal,
format_timestamp,
proc_fmt,
Expand Down
21 changes: 0 additions & 21 deletions src/lenskit/logging/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,6 @@
LOGGED_ERRORS = deque([], 5)


def error_was_logged(exc: Exception) -> bool:
"""
Query whether an exception was logged.
"""
return exc in LOGGED_ERRORS


def record_logged_error(logger: Any, method: str, event_dict: EventDict):
"""
Keep a record of recently-logged errors to avoid duplicate rendering in some
cases.
"""

exc = event_dict.get("exc_info", None)

if isinstance(exc, Exception):
LOGGED_ERRORS.append(exc)

return event_dict


def remove_internal(logger: Any, method: str, event_dict: EventDict) -> EventDict:
"""
Filter out “internal” attrs (beginning with ``_``) for console logging.
Expand Down
16 changes: 15 additions & 1 deletion src/lenskit/logging/progress/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@ class Progress:
def __init__(self, *args: Any, **kwargs: Any):
pass

def update(self, advance: int = 1, **kwargs: float | int | str):
def update(
self,
advance: int = 1,
completed: int | None = None,
total: int | None = None,
**kwargs: float | int | str,
):
"""
Update the progress bar.

Args:
advance:
The amount to advance by.
completed:
The number completed; this overrides ``advance``.
total:
A new total, to update the progress bar total.
"""
pass

Expand Down
15 changes: 13 additions & 2 deletions src/lenskit/logging/progress/_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,22 @@
[f"{name}: {field_format(name, fs)}" for (name, fs) in fields.items()]
)

def update(self, advance: int = 1, **kwargs: float | int | str):
def update(

Check warning on line 70 in src/lenskit/logging/progress/_notebook.py

View check run for this annotation

Codecov / codecov/patch

src/lenskit/logging/progress/_notebook.py#L70

Added line #L70 was not covered by tests
self,
advance: int = 1,
completed: int | None = None,
total: int | None = None,
**kwargs: float | int | str,
):
"""
Update the progress bar.
"""
self.current += advance
if total is not None:
self.total = total
if completed is not None:
self.current = completed

Check warning on line 83 in src/lenskit/logging/progress/_notebook.py

View check run for this annotation

Codecov / codecov/patch

src/lenskit/logging/progress/_notebook.py#L80-L83

Added lines #L80 - L83 were not covered by tests
else:
self.current += advance

Check warning on line 85 in src/lenskit/logging/progress/_notebook.py

View check run for this annotation

Codecov / codecov/patch

src/lenskit/logging/progress/_notebook.py#L85

Added line #L85 was not covered by tests
now = perf_counter()
if now - self._last_update >= 0.1 or (self.total and self.current >= self.total):
self.widget.value = self.current
Expand Down
16 changes: 14 additions & 2 deletions src/lenskit/logging/progress/_rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,24 @@
]
)

def update(self, advance: int = 1, **kwargs: float | int | str):
def update(
self,
advance: int = 1,
completed: int | None = None,
total: int | None = None,
**kwargs: float | int | str,
):
extra = ""
if self._field_format:
extra = self._field_format.format(**kwargs)
if _progress is not None:
_progress.update(self._task, advance=advance, extra=extra) # type: ignore
_progress.update(

Check warning on line 81 in src/lenskit/logging/progress/_rich.py

View check run for this annotation

Codecov / codecov/patch

src/lenskit/logging/progress/_rich.py#L81

Added line #L81 was not covered by tests
self._task, # type: ignore
advance=advance if completed is None else None,
completed=completed,
total=total,
extra=extra,
)

def finish(self):
_remove_bar(self)
Expand Down
Loading