Skip to content

Config

The configuration class that handles configuration files.

Source code in pyrays/src/config/config.py
class Config:
    """The configuration class that handles configuration files."""

    def __init__(self, filename: str) -> None:
        """Initialize the Config class by loading configurations from a given file.

        Args:
            filename (str): The name of the configuration file.

        """
        # ensure a file exists and is actually read
        if filename:
            try:
                with open(filename, "r") as f:
                    _config = json.load(f)
                    for key, val in _config.items():
                        if isinstance(val, dict):
                            for subkey, subval in val.items():
                                subval = _try_convert_to_number(subval)
                                setattr(self, subkey, subval)
                        else:
                            val = _try_convert_to_number(val)
                            setattr(self, key, val)
            except FileNotFoundError:
                raise FileNotFoundError(f"Config file {filename} not found.")

    def __getattr__(self, name: str):
        """Raise AttributeError for missing attributes.

        Args:
            name: The name of the attribute.

        Raises:
            AttributeError: Always raised for missing attributes.

        """
        raise AttributeError(
            f"'{type(self).__name__}' object has no attribute '{name}'"
        )

__getattr__(name)

Raise AttributeError for missing attributes.

Parameters:

Name Type Description Default
name str

The name of the attribute.

required

Raises:

Type Description
AttributeError

Always raised for missing attributes.

Source code in pyrays/src/config/config.py
def __getattr__(self, name: str):
    """Raise AttributeError for missing attributes.

    Args:
        name: The name of the attribute.

    Raises:
        AttributeError: Always raised for missing attributes.

    """
    raise AttributeError(
        f"'{type(self).__name__}' object has no attribute '{name}'"
    )

__init__(filename)

Initialize the Config class by loading configurations from a given file.

Parameters:

Name Type Description Default
filename str

The name of the configuration file.

required
Source code in pyrays/src/config/config.py
def __init__(self, filename: str) -> None:
    """Initialize the Config class by loading configurations from a given file.

    Args:
        filename (str): The name of the configuration file.

    """
    # ensure a file exists and is actually read
    if filename:
        try:
            with open(filename, "r") as f:
                _config = json.load(f)
                for key, val in _config.items():
                    if isinstance(val, dict):
                        for subkey, subval in val.items():
                            subval = _try_convert_to_number(subval)
                            setattr(self, subkey, subval)
                    else:
                        val = _try_convert_to_number(val)
                        setattr(self, key, val)
        except FileNotFoundError:
            raise FileNotFoundError(f"Config file {filename} not found.")