Skip to content

Package

DHyDAMO Package

Top-level package and shared modules.

D-HyDAMO: build D-HYDRO models (1D2D, RR, RTC) from HyDAMO data with HYDROLIB-core.

__version__ = '0.5.1' module-attribute

Configure logging for hydrolib.dhydamo without adding duplicate handlers.

Calling it again with the same stream or file does not add another handler, so a notebook cell can be run more than once.

Parameters:

Name Type Description Default
level int or str

Lowest level that is logged, e.g. logging.DEBUG or "WARNING". Defaults to logging.INFO.

INFO
fmt str

Format of each message, see logging.Formatter. Defaults to DEFAULT_FORMAT.

DEFAULT_FORMAT
stream bool

Log to the screen (sys.stdout). Defaults to True. Messages are then no longer passed on to the root logger, so they do not appear twice.

True
filepath str or Path

Log file, in UTF-8. Missing folders are created. Defaults to None: no log file.

None
logger_name str

Logger to configure. Defaults to "hydrolib.dhydamo".

'hydrolib.dhydamo'
color bool

Color the messages on the screen by level, e.g. warnings yellow and errors red. Needs a terminal or notebook that shows ANSI colors. The log file is never colored. Defaults to False.

False
mode (a, w)

"a" adds to an existing log file, "w" starts with an empty one. Defaults to "a".

"a"

Returns:

Type Description
Logger

The configured logger.

Source code in hydrolib/dhydamo/logging.py
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def configure_logging(
    level: int | str = logging.INFO,
    fmt: str = DEFAULT_FORMAT,
    stream: bool = True,
    filepath: str | Path | None = None,
    logger_name: str = "hydrolib.dhydamo",
    color: bool = False,
    mode: Literal["a", "w"] = "a",
) -> logging.Logger:
    """Configure logging for hydrolib.dhydamo without adding duplicate handlers.

    Calling it again with the same stream or file does not add another handler, so a notebook
    cell can be run more than once.

    Parameters
    ----------
    level : int or str, optional
        Lowest level that is logged, e.g. ``logging.DEBUG`` or ``"WARNING"``. Defaults to
        ``logging.INFO``.
    fmt : str, optional
        Format of each message, see ``logging.Formatter``. Defaults to ``DEFAULT_FORMAT``.
    stream : bool, optional
        Log to the screen (``sys.stdout``). Defaults to True. Messages are then no longer passed
        on to the root logger, so they do not appear twice.
    filepath : str or Path, optional
        Log file, in UTF-8. Missing folders are created. Defaults to None: no log file.
    logger_name : str, optional
        Logger to configure. Defaults to ``"hydrolib.dhydamo"``.
    color : bool, optional
        Color the messages on the screen by level, e.g. warnings yellow and errors red. Needs a
        terminal or notebook that shows ANSI colors. The log file is never colored. Defaults to
        False.
    mode : {"a", "w"}, optional
        ``"a"`` adds to an existing log file, ``"w"`` starts with an empty one. Defaults to
        ``"a"``.

    Returns
    -------
    logging.Logger
        The configured logger.
    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(level)
    formatter = logging.Formatter(fmt)

    has_stream_handler = False
    for h in logger.handlers:
        # a FileHandler is also a StreamHandler, so leave those out when looking for a screen handler
        if isinstance(h, logging.FileHandler):
            continue
        if isinstance(h, logging.StreamHandler):
            has_stream_handler = True
            break

    if stream and not has_stream_handler:
        handler = logging.StreamHandler(sys.stdout)
        handler.setLevel(level)
        if color:
            handler.setFormatter(ColorFormatter(fmt))
        else:
            handler.setFormatter(formatter)
        logger.addHandler(handler)
        logger.propagate = False

    if filepath:
        directory = os.path.dirname(filepath)
        if directory and not os.path.isdir(directory):
            os.makedirs(directory)
        # FileHandler stores the absolute path
        path = os.path.abspath(filepath)
        if not any(
            isinstance(h, logging.FileHandler) and h.baseFilename == path
            for h in logger.handlers
        ):
            file_handler = logging.FileHandler(path, mode=mode, encoding="utf-8")
            file_handler.setLevel(level)
            file_handler.setFormatter(formatter)
            logger.addHandler(file_handler)

    return logger

Empty package, left over from the initial file structure (2022).