File size: 3,972 Bytes
cc9c7ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import yaml
import copy
from src.utils.mapper import configmapper


def load_yaml(path):
    """
    Function to load a yaml file and
    return the collected dict(s)

    Parameters
    ----------
    path : str
        The path to the yaml config file

    Returns
    -------
    result : dict
        The dictionary from the config file
    """

    assert isinstance(path, str), "Provided path is not a string"
    try:
        f = open(path, "r")
        result = yaml.load(f, Loader=yaml.Loader)
    except FileNotFoundError as e:
        # Adding this for future functionality
        raise e
    return result


def convert_params_to_dict(params):
    dic = {}
    for k, v in params.as_dict():
        try:
            obj = configmapper.get_object("params", v)
            dic[k] = v
        except:
            print(
                f"Undefined {v} for the given key: {k} in mapper        ,storing original value"
            )
            dic[k] = v
        return value


class Config:
    """Config Class to be used with YAML configuration files

    This class can be used to address keys as attributes.
    Ensure that there are no spaces between the keys.
    Only objects of type dict can be converted to config.

    Attributes
    ----------
    _config : dict,
        The dictionary which is formed from the
        yaml file or custom dictionary

    Methods
    -------
    as_dict(),
        Return the config object as dictionary

        Possible update:
        ## Can be converted using __getattr__ to use **kwargs
        ## with the Config object directly.

    set_value(attr,value)
        Set the value of a particular attribute.
    """

    def __init__(self, *, path=None, dic=None):
        """
        Initializer for the Config class

        Needs either path or the dict object to create the config

        Parameters
        ----------
        path: str, optional
            The path to the config YAML file.
            Default value is None.
        dic : dict, optional
            The dictionary containing the configuration.
            Default value is None.
        """
        if path:
            self._config = load_yaml(path)
        elif dict:
            self._config = dic
        else:
            raise Exception("Need either path or dict object to instantiate object.")
        # self.keys = self._config.keys()

    def __getattr__(self, attr):
        """
        Get method for Config class. Helps get keys as attributes.

        Parameters
        ----------
        attr: The attribute name passed as <object>.attr

        Returns
        -------
        self._config[attr]: object or Config object
            The value of the given key if it exists.
            If the value is a dict object,
            a Config object of that dict is returned.
            Otherwise, the exact value is returned.

        Raises
        ------

        KeyError() if the given key is not defined.
        """
        if attr in self._config:
            if isinstance(self._config[attr], dict):
                return Config(dic=self._config[attr])
            else:
                return self._config[attr]
        else:
            raise KeyError(f"Key:{attr} not defined.")

    def set_value(self, attr, value):
        """
        Set method for Config class. Helps set keys in the _config.

        Parameters
        ----------
        attr: The attribute name passed as <object>.attr
        value: The value to be stored as the attr.
        """
        self._config[attr] = value

    def __str__(self):
        """Function to print the dictionary
        contained in the object."""
        return self._config.__str__()

    def __repr__(self):
        return f"Config(dic={self._config})"

    def __deepcopy__(self, memo):
        return Config(dic=copy.deepcopy(self._config))

    def as_dict(self):
        """Function to get the config as dictionary object"""
        return dict(self._config)