Skip to content

medcat.utils.config_utils

Classes:

Functions:

IllegalConfigPathException

IllegalConfigPathException(target_path: str)

Bases: ValueError

Source code in medcat-v2/medcat/utils/config_utils.py
31
32
33
def __init__(self, target_path: str):
    super().__init__(
        f"Config has no target path: {target_path}")

temp_changed_config

temp_changed_config(config: BaseModel, target: str, value: Any)

Context manager to change the config temporarily (within).

Parameters:

  • config

    (BaseModel) –

    The config in question.

  • target

    (str) –

    The attribute name to change.

  • value

    (Any) –

    The temporary value to use.

Raises:

Source code in medcat-v2/medcat/utils/config_utils.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@contextmanager
def temp_changed_config(config: BaseModel, target: str, value: Any):
    """Context manager to change the config temporarily (within).

    Args:
        config (BaseModel): The config in question.
        target (str): The attribute name to change.
        value (Any): The temporary value to use.

    Raises:
        IllegalConfigPathException: If no previous value is available.
    """
    try:
        prev_value = getattr(config, target)
    except AttributeError as e:
        raise IllegalConfigPathException(target) from e
    setattr(config, target, value)
    try:
        yield
    finally:
        setattr(config, target, prev_value)