-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d3d552
commit a54c319
Showing
5 changed files
with
164 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
# pylint: disable=global-statement,unused-argument | ||
"""Provide a singleton progress reporter implementation. | ||
The interface is inspired by `tqdm <https://github.com/tqdm/tqdm>`, | ||
and indeed a valid implementation is:: | ||
from tqdm import tqdm | ||
set_progress_reporter(tqdm) | ||
""" | ||
from contextlib import contextmanager | ||
from typing import Any, Callable, ContextManager, Iterator, Optional | ||
|
||
__all__ = ('get_progress_reporter', 'set_progress_reporter', 'progress_reporter_base', 'ProgressIncrementerBase') | ||
|
||
|
||
class ProgressIncrementerBase: | ||
"""A base class for incrementing a progress reporter.""" | ||
|
||
def set_description_str(self, text: Optional[str] = None, refresh: bool = True): | ||
"""Set the text shown by the progress reporter. | ||
:param text: The text to show | ||
:param refresh: Force refresh of the progress reporter | ||
""" | ||
|
||
def update(self, n: int = 1): # pylint: disable=invalid-name | ||
"""Update the progress counter. | ||
:param n: Increment to add to the internal counter of iterations | ||
""" | ||
|
||
|
||
@contextmanager | ||
def progress_reporter_base(*, | ||
total: int, | ||
desc: Optional[str] = None, | ||
**kwargs: Any) -> Iterator[ProgressIncrementerBase]: | ||
"""A context manager for providing a progress reporter for a process. | ||
Example Usage:: | ||
with progress_reporter(total=10, desc="A process:") as progress: | ||
for i in range(10): | ||
progress.set_description_str(f"A process: {i}") | ||
progress.update() | ||
:param total: The number of expected iterations. | ||
:param desc: A description of the process | ||
:yield: A class for incrementing the progress reporter | ||
""" | ||
yield ProgressIncrementerBase() | ||
|
||
|
||
PROGRESS_REPORTER = progress_reporter_base | ||
|
||
|
||
def get_progress_reporter() -> Callable[..., ContextManager[Any]]: | ||
"""Return the progress reporter | ||
Example Usage:: | ||
with get_progress_reporter()(total=10, desc="A process:") as progress: | ||
for i in range(10): | ||
progress.set_description_str(f"A process: {i}") | ||
progress.update() | ||
""" | ||
global PROGRESS_REPORTER | ||
return PROGRESS_REPORTER # type: ignore | ||
|
||
|
||
def set_progress_reporter(reporter: Optional[Callable[..., ContextManager[Any]]] = None): | ||
"""Set the progress reporter implementation | ||
:param reporter: A context manager for providing a progress reporter for a process. | ||
If None, reset to default null reporter | ||
The reporter should be a context manager that implements the | ||
:func:`~aiida.common.progress_reporter.progress_reporter_base` interface. | ||
Example Usage:: | ||
with get_progress_reporter()(total=10, desc="A process:") as progress: | ||
for i in range(10): | ||
progress.set_description_str(f"A process: {i}") | ||
progress.update() | ||
""" | ||
global PROGRESS_REPORTER | ||
PROGRESS_REPORTER = reporter or progress_reporter_base # type: ignore |
Oops, something went wrong.