From ccd819d7a90c5342be915e7419d3fafda3ca34ab Mon Sep 17 00:00:00 2001 From: Sergio Hernandez Date: Mon, 19 Feb 2024 23:23:28 +0000 Subject: [PATCH] feat(progress): progress bar wrapper of `rich.progress` --- .gitignore | 4 ++++ README.md | 14 +++++++++++++- mloggers/progress.py | 37 +++++++++++++++++++++++++++++++++++++ pyproject.toml | 4 ++-- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 mloggers/progress.py diff --git a/.gitignore b/.gitignore index 68bc17f..de1461e 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,7 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# Rendered markdown +*.html +README_files diff --git a/README.md b/README.md index d2b95ce..2b48788 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This package offers a collection of loggers well-suited for machine learning exp ## Getting started -You can download the package via `pip install mloggers`. Dependencies include: +You can download the package via `pip install mloggers`. Python version $\geq$ 3.10 is required. Dependencies include: - `aenum` - `numpy` @@ -79,6 +79,18 @@ In the case of the `MultiLogger`, the methods above have the additional optional Masks are used by the `MultiLogger` to filter loggers which are not supposed to record a given message. At the time of initialization, you can define a default mask to use for all messages for which a mask is not specified when calling `MultiLogger.log(message, level, mask)` or the level-specific variants. To create a mask, simply pass as argument a list of the class references for the loggers you would like to mask out. +### Progress bars + +You can make use of a pre-configured wrapper of the progress bars provided by the package `rich.progress`. The wrapper is provided via the function `mloggers.progress.log_progress`. Example usage: + +```python +import time +from mloggers.progress import log_progress + +for _ in log_progress(range(100)): + time.sleep(0.1) +``` + ### Customized loggers You can extend the base class `Logger` in order to create a custom logger to suit your own needs. Make sure to implement all abstract methods. diff --git a/mloggers/progress.py b/mloggers/progress.py new file mode 100644 index 0000000..62e0bf2 --- /dev/null +++ b/mloggers/progress.py @@ -0,0 +1,37 @@ +from typing import Iterable, Sequence, Union + +from rich.progress import ( + BarColumn, + MofNCompleteColumn, + Progress, + TextColumn, + TimeElapsedColumn, + TimeRemainingColumn, +) + + +def log_progress(iterable: Union[Iterable, Sequence]): + """ + Log an iterable or sequence using a progress bar. + Wraps `rich.progress.Progress.track`. + + ### Parameters + ---------- + `iterable`: the iterable whose progress to log. + + ### Returns + ------- + The tracked iterable. + """ + + bar = Progress( + TextColumn("[progress.percentage]{task.percentage:>3.0f}%"), + BarColumn(), + MofNCompleteColumn(), + TextColumn("•"), + TimeElapsedColumn(), + TextColumn("•"), + TimeRemainingColumn(), + ) + bar.__enter__() + return bar.track(iterable) diff --git a/pyproject.toml b/pyproject.toml index 34ab6ec..933d40d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "mloggers" -version = "1.0.2" +version = "1.1.0" authors = [ { name = "Sergio Hernandez Gutierrez", email = "contact.sergiohernandez@gmail.com" }, ] @@ -16,7 +16,7 @@ classifiers = [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] -dependencies = ["aenum", "numpy", "omegaconf", "termcolor", "wandb"] +dependencies = ["aenum", "numpy", "omegaconf", "termcolor", "wandb", "rich"] [project.urls] Homepage = "https://github.com/serhez/mloggers"