Esempio n. 1
0
def cast(k, v, pytype, ctype, default):
    try:
        # if default is None:
        #     raise TypeError
        val = ctype(pytype(v))
    except KeyError:
        val = ctype(default)
        cout.cout_wrap("--- The variable " + k + " has no given value, using the default " + default, 2)
    except TypeError:
        raise exceptions.NoDefaultValueException(k)
    except ValueError:
        val = ctype(v.value)
    return val
Esempio n. 2
0
def to_custom_types(dictionary, types, default):
    for k, v in types.items():
        if v == 'int':
            try:
                dictionary[k] = cast(k, dictionary[k], int, ct.c_int,
                                     default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], int, ct.c_int, default[k])

        elif v == 'float':
            try:
                dictionary[k] = cast(k, dictionary[k], float, ct.c_double,
                                     default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], float, ct.c_double,
                                     default[k])

        elif v == 'str':
            try:
                dictionary[k] = cast(k, dictionary[k], str, str, default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], eval(v), eval(v),
                                     default[k])

        elif v == 'bool':
            try:
                dictionary[k] = cast(k, dictionary[k], str2bool, ct.c_bool,
                                     default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], str2bool, ct.c_bool,
                                     default[k])

        elif v == 'list(str)':
            try:
                # if isinstance(dictionary[k], list):
                #     continue
                # dictionary[k] = dictionary[k].split(',')
                # getting rid of leading and trailing spaces
                dictionary[k] = list(map(lambda x: x.strip(), dictionary[k]))
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()

        elif v == 'list(float)':
            try:
                if isinstance(dictionary[k], np.ndarray):
                    continue
                if isinstance(dictionary[k], list):
                    for i in range(len(dictionary[k])):
                        dictionary[k][i] = float(dictionary[k][i])
                    dictionary[k] = np.array(dictionary[k])
                    continue
                # dictionary[k] = dictionary[k].split(',')
                # # getting rid of leading and trailing spaces
                # dictionary[k] = list(map(lambda x: x.strip(), dictionary[k]))
                if dictionary[k].find(',') < 0:
                    dictionary[k] = np.fromstring(dictionary[k].strip('[]'),
                                                  sep=' ',
                                                  dtype=ct.c_double)
                else:
                    dictionary[k] = np.fromstring(dictionary[k].strip('[]'),
                                                  sep=',',
                                                  dtype=ct.c_double)
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()

        elif v == 'dict':
            if not isinstance(dictionary[k], dict):
                raise TypeError
Esempio n. 3
0
def to_custom_types(dictionary, types, default, options=dict(), no_ctype=False):
    for k, v in types.items():
        if v == 'int':
            if no_ctype:
                data_type = int
            else:
                data_type = ct.c_int
            try:
                dictionary[k] = cast(k, dictionary[k], int, data_type, default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], int, data_type, default[k])
                notify_default_value(k, dictionary[k])

        elif v == 'float':
            if no_ctype:
                data_type = float
            else:
                data_type = ct.c_double
            try:
                dictionary[k] = cast(k, dictionary[k], float, data_type, default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], float, data_type, default[k])
                notify_default_value(k, dictionary[k])

        elif v == 'str':
            try:
                dictionary[k] = cast(k, dictionary[k], str, str, default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], eval(v), eval(v), default[k])
                notify_default_value(k, dictionary[k])

        elif v == 'bool':
            if no_ctype:
                data_type = bool
            else:
                data_type = ct.c_bool
            try:
                dictionary[k] = cast(k, dictionary[k], str2bool, data_type, default[k])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = cast(k, default[k], str2bool, data_type, default[k])
                notify_default_value(k, dictionary[k])

        elif v == 'list(str)':
            try:
                # if isinstance(dictionary[k], list):
                #     continue
                # dictionary[k] = dictionary[k].split(',')
                # getting rid of leading and trailing spaces
                dictionary[k] = list(map(lambda x: x.strip(), dictionary[k]))
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()
                notify_default_value(k, dictionary[k])

        elif v == 'list(dict)':
            try:
                # if isinstance(dictionary[k], list):
                #     continue
                # dictionary[k] = dictionary[k].split(',')
                # getting rid of leading and trailing spaces
                for i in range(len(dictionary[k])):
                    dictionary[k][i] = ast.literal_eval(dictionary[k][i])
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()
                notify_default_value(k, dictionary[k])

        elif v == 'list(float)':
            try:
                dictionary[k]
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()
                notify_default_value(k, dictionary[k])

            if isinstance(dictionary[k], np.ndarray):
                continue
            if isinstance(dictionary[k], list):
                for i in range(len(dictionary[k])):
                    dictionary[k][i] = float(dictionary[k][i])
                dictionary[k] = np.array(dictionary[k])
                continue
            # dictionary[k] = dictionary[k].split(',')
            # # getting rid of leading and trailing spaces
            # dictionary[k] = list(map(lambda x: x.strip(), dictionary[k]))
            if dictionary[k].find(',') < 0:
                dictionary[k] = np.fromstring(dictionary[k].strip('[]'), sep=' ', dtype=ct.c_double)
            else:
                dictionary[k] = np.fromstring(dictionary[k].strip('[]'), sep=',', dtype=ct.c_double)

        elif v == 'list(int)':
            try:
                dictionary[k]
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()
                notify_default_value(k, dictionary[k])

            if isinstance(dictionary[k], np.ndarray):
                continue
            if isinstance(dictionary[k], list):
                for i in range(len(dictionary[k])):
                    dictionary[k][i] = int(dictionary[k][i])
                dictionary[k] = np.array(dictionary[k])
                continue
            # dictionary[k] = dictionary[k].split(',')
            # # getting rid of leading and trailing spaces
            # dictionary[k] = list(map(lambda x: x.strip(), dictionary[k]))
            if dictionary[k].find(',') < 0:
                dictionary[k] = np.fromstring(dictionary[k].strip('[]'), sep=' ').astype(ct.c_int)
            else:
                dictionary[k] = np.fromstring(dictionary[k].strip('[]'), sep=',').astype(ct.c_int)

        elif v == 'list(complex)':
            try:
                dictionary[k]
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()
                notify_default_value(k, dictionary[k])

            if isinstance(dictionary[k], np.ndarray):
                continue
            if isinstance(dictionary[k], list):
                for i in range(len(dictionary[k])):
                    dictionary[k][i] = float(dictionary[k][i])
                dictionary[k] = np.array(dictionary[k])
                continue
            # dictionary[k] = dictionary[k].split(',')
            # # getting rid of leading and trailing spaces
            # dictionary[k] = list(map(lambda x: x.strip(), dictionary[k]))
            if dictionary[k].find(',') < 0:
                dictionary[k] = np.fromstring(dictionary[k].strip('[]'), sep=' ').astype(complex)
            else:
                dictionary[k] = np.fromstring(dictionary[k].strip('[]'), sep=',').astype(complex)

        elif v == 'dict':
            try:
                if not isinstance(dictionary[k], dict):
                    raise TypeError
            except KeyError:
                if default[k] is None:
                    raise exceptions.NoDefaultValueException(k)
                dictionary[k] = default[k].copy()
                notify_default_value(k, dictionary[k])
        else:
            raise TypeError('Variable %s has an unknown type (%s) that cannot be casted' % (k, v))

    check_settings(dictionary, types, options)