Skip to content

Commit

Permalink
feat(progress): progress bar wrapper of rich.progress
Browse files Browse the repository at this point in the history
  • Loading branch information
serhez committed Feb 19, 2024
1 parent 24ecec7 commit ccd819d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions mloggers/progress.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" },
]
Expand All @@ -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"
Expand Down

0 comments on commit ccd819d

Please sign in to comment.