Ejemplo n.º 1
0
def convert_dict(dictionary):
    converted_dict = {}
    for key, item in dictionary.items():
        if isinstance(item, dict):
            item = convert_dict(item)
        else:
            item = convert(item)
        converted_dict[key] = item
    return converted_dict
Ejemplo n.º 2
0
    def __convert(self, value, d_type, default, sub_space=None):
        """
        AUTHORS:
        --------
        :author: Samuel Westlake

        DESCRIPTION:
        ------------
        Converts a given value to a given data type, and returns the result.
        If the value can not be converted, the given default value is returned.
        NB: If d_type is given in a list, e.g. [str], it is assume that value should be a list and each item in value
        will be converted to the given data type. If any item in the list cannot be converted, None will be returned.

        PARAMETERS:
        -----------
        :param value: the value to be converted
        :param d_type: the data type to convert the value to
        :param default: the default value to return if the current value
        :param sub_space: the list of strings that defines the current sub-space

        RETURN:
        -------
        :return: new_value
        """
        sub_space = [] if sub_space is None else sub_space
        sub_space = DEEP_CONFIG_DIVIDER.join(sub_space)

        # If the value is not set
        if value is None and default is not None:
            # Notify user that default is being used
            if default == {}:
                new_value = Namespace()
            else:
                new_value = default
                if not default == []:
                    Notification(DEEP_NOTIF_WARNING, DEEP_MSG_CONFIG_NOT_SET % (sub_space, default))
        elif d_type is dict:
            new_value = Namespace(convert_dict(value.get_all()))
        else:
            new_value = convert(value, d_type)
            if new_value is None:
                # Notify user that default is being used
                Notification(
                    DEEP_NOTIF_WARNING, DEEP_MSG_CONFIG_NOT_CONVERTED % (
                        sub_space,
                        value,
                        self.__get_dtype_name(d_type),
                        default
                    )
                )
        return new_value