Skip to content

datamol.log

without_rdkit_log

Context manager to disable RDKit logs. By default all logs are disabled.

Example:

import datamol as dm

with dm.without_rdkit_log():
    mol = dm.to_mol("CCCCO")  # potential RDKit logs won't show
Source code in datamol/log.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
class without_rdkit_log:
    """Context manager to disable RDKit logs. By default all logs are disabled.

    Example:

    ```python
    import datamol as dm

    with dm.without_rdkit_log():
        mol = dm.to_mol("CCCCO")  # potential RDKit logs won't show
    ```
    """

    def __init__(
        self,
        mute_errors: bool = True,
        mute_warning: bool = True,
        mute_info: bool = True,
        mute_debug: bool = True,
        enable: bool = True,
    ):
        if enable is False:
            mute_errors = False
            mute_warning = False
            mute_info = False
            mute_debug = False

        # Get current log state
        self.previous_status = self._get_log_status()

        # Init the desired log state to apply during in the context
        self.desired_status = {}
        self.desired_status["rdApp.error"] = not mute_errors
        self.desired_status["rdApp.warning"] = not mute_warning
        self.desired_status["rdApp.debug"] = not mute_debug
        self.desired_status["rdApp.info"] = not mute_info

    def _get_log_status(self):
        """Get the current log status of RDKit logs."""
        log_status = rdBase.LogStatus()
        log_status = {st.split(":")[0]: st.split(":")[1] for st in log_status.split("\n")}
        log_status = {k: True if v == "enabled" else False for k, v in log_status.items()}
        return log_status

    def _apply_log_status(self, log_status):
        """Apply an RDKit log status."""
        for k, v in log_status.items():
            if v is True:
                rdBase.EnableLog(k)
            else:
                rdBase.DisableLog(k)

    def __enter__(self):
        self._apply_log_status(self.desired_status)

    def __exit__(self, *args, **kwargs):
        self._apply_log_status(self.previous_status)

disable_rdkit_log()

Disable all rdkit logs.

Source code in datamol/log.py
65
66
67
68
def disable_rdkit_log():
    """Disable all rdkit logs."""
    for log_level in RDLogger._levels:
        rdBase.DisableLog(log_level)

enable_rdkit_log()

Enable all rdkit logs.

Source code in datamol/log.py
71
72
73
74
def enable_rdkit_log():
    """Enable all rdkit logs."""
    for log_level in RDLogger._levels:
        rdBase.EnableLog(log_level)

no_rdkit_log(func=None, *, mute_errors=True, mute_warning=True, mute_info=True, mute_debug=True, enable=True)

Decorator to disable RDKit logs.

This decorator can be used to suppress RDKit logs when executing a specific function. By default, all log levels (error, warning, info, and debug) are muted.

Parameters:

Name Type Description Default
mute_errors

Whether to mute error logs (default is True).

True
mute_warning

Whether to mute warning logs (default is True).

True
mute_info

Whether to mute info logs (default is True).

True
mute_debug

Whether to mute debug logs (default is True).

True
enable bool

Whether to enable the log muting (default is True). If set to False, no logs will be muted.

True

Example:

@no_rdkit_log()
def example_function():
    # Your function code here
    pass

example_function()  # RDKit logs won't show during this function's execution

Source code in datamol/log.py
 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
112
113
114
115
116
117
118
119
120
121
122
123
124
def no_rdkit_log(
    func=None,
    *,
    mute_errors: bool = True,
    mute_warning: bool = True,
    mute_info: bool = True,
    mute_debug: bool = True,
    enable: bool = True,
):
    """Decorator to disable RDKit logs.

    This decorator can be used to suppress RDKit logs when executing a specific function.
    By default, all log levels (error, warning, info, and debug) are muted.

    Args:
        mute_errors : Whether to mute error logs (default is True).
        mute_warning : Whether to mute warning logs (default is True).
        mute_info : Whether to mute info logs (default is True).
        mute_debug : Whether to mute debug logs (default is True).
        enable: Whether to enable the log muting (default is True). If set to False, no logs will be muted.

    Example:
    ```python
    @no_rdkit_log()
    def example_function():
        # Your function code here
        pass

    example_function()  # RDKit logs won't show during this function's execution
    ```
    """

    if func is None:
        return lambda f: no_rdkit_log(
            f,
            mute_errors=mute_errors,
            mute_warning=mute_warning,
            mute_info=mute_info,
            mute_debug=mute_debug,
            enable=enable,
        )

    @wraps(func)
    def wrapper(*args, **kwargs):
        with without_rdkit_log(mute_errors, mute_warning, mute_info, mute_debug, enable):
            return func(*args, **kwargs)

    return wrapper