How to use the swat.exceptions.SWATOptionError function in swat

To help you get started, we’ve selected a few swat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sassoftware / python-swat / swat / utils / config.py View on Github external
The maximum length of the string
    min_length : int, optional
        The minimum length of the string
    valid_values : list of strings, optional
        List of the only possible values

    Returns
    -------
    string
        The validated string value

    '''
    try:
        out = a2u(value)
    except Exception:
        raise SWATOptionError('Could not convert string value to unicode')

    if max_length is not None and len(out) > max_length:
        raise SWATOptionError('%s is longer than the maximum length of %s' %
                              (out, max_length))

    if min_length is not None and len(out) < min_length:
        raise SWATOptionError('%s is shorter than the minimum length of %s' %
                              (out, min_length))

    if pattern is not None and not re.search(pattern, out):
        raise SWATOptionError('%s does not match pattern %s' % (out, pattern))

    if valid_values is not None and out not in valid_values:
        raise SWATOptionError('%s is not one of the possible values: %s' %
                              (out, ', '.join(valid_values)))
github sassoftware / python-swat / swat / utils / config.py View on Github external
-------
    string
        The validated string value

    '''
    try:
        out = a2u(value)
    except Exception:
        raise SWATOptionError('Could not convert string value to unicode')

    if max_length is not None and len(out) > max_length:
        raise SWATOptionError('%s is longer than the maximum length of %s' %
                              (out, max_length))

    if min_length is not None and len(out) < min_length:
        raise SWATOptionError('%s is shorter than the minimum length of %s' %
                              (out, min_length))

    if pattern is not None and not re.search(pattern, out):
        raise SWATOptionError('%s does not match pattern %s' % (out, pattern))

    if valid_values is not None and out not in valid_values:
        raise SWATOptionError('%s is not one of the possible values: %s' %
                              (out, ', '.join(valid_values)))

    return out
github sassoftware / python-swat / swat / utils / config.py View on Github external
----------
    *args : string / any pairs
        The name and value of an option in consecutive arguments (not tuples)
    **kwargs : dict
        Arbitrary keyword / value pairs

    Returns
    -------
    None

    '''
    for key, value in iteroptions(*args, **kwargs):
        key = _get_option_leaf_node(key)
        opt = _config[key]
        if not isinstance(opt, SWATOption):
            raise SWATOptionError('%s is not a valid option name' % key)
        opt.set(value)
github sassoftware / python-swat / swat / utils / config.py View on Github external
string
        The full key name of the option

    Raises
    ------
    SWATOptionError
        If more than one option matches

    '''
    flatkeys = list(_config.flatkeys())
    key = key.lower()
    if key in flatkeys:
        return key
    keys = [k for k in flatkeys if k.endswith('.' + key)]
    if len(keys) > 1:
        raise SWATOptionError('There is more than one option with the name %s.' % key)
    if not keys:
        raise SWATOptionError('%s is not a valid option name.' % key)
    return keys[0]
github sassoftware / python-swat / swat / utils / config.py View on Github external
-------
    float
        The validated floating point value

    '''
    try:
        out = float(value)
    except Exception:
        raise SWATOptionError('Could not convert %s to a float' % value)

    if minimum is not None:
        if out < minimum:
            raise SWATOptionError('%s is smaller than the minimum value of %s' %
                                  (out, minimum))
        if exclusive_minimum and out == minimum:
            raise SWATOptionError('%s is equal to the exclusive nimum value of %s' %
                                  (out, minimum))

    if maximum is not None:
        if out > maximum:
            raise SWATOptionError('%s is larger than the maximum value of %s' %
                                  (out, maximum))
        if exclusive_maximum and out == maximum:
            raise SWATOptionError('%s is equal to the exclusive maximum value of %s' %
                                  (out, maximum))

    if multiple_of is not None and (out % int(multiple_of)) != 0:
        raise SWATOptionError('%s is not a multiple of %s' % (out, multiple_of))

    return out
github sassoftware / python-swat / swat / utils / config.py View on Github external
Parameters
    ----------
    key : string
        The name of the option

    Returns
    -------
    any
        The value of the option

    '''
    key = _get_option_leaf_node(key)
    opt = _config[key]
    if not isinstance(opt, SWATOption):
        raise SWATOptionError('%s is not a valid option name' % key)
    return opt.get()
github sassoftware / python-swat / swat / utils / config.py View on Github external
out = []

    if not keys:
        keys = sorted(_config.flatkeys())
    else:
        newkeys = []
        for k in keys:
            try:
                newkeys.append(_get_option_leaf_node(k))
            except SWATOptionError:
                newkeys.append(k)

    for key in keys:

        if key not in _config:
            raise SWATOptionError('%s is not a valid option name' % key)

        opt = _config[key]
        if isinstance(opt, xdict):
            desc = describe_option(*['%s.%s' % (key, x)
                                   for x in opt.flatkeys()], _print_desc=_print_desc)
            if desc is not None:
                out.append(desc)
            continue

        if _print_desc:
            print(opt.__doc__)
            print('')
        else:
            out.append(opt.__doc__)

    if not _print_desc:
github sassoftware / python-swat / swat / utils / config.py View on Github external
valid_values : list of strings, optional
        List of the only possible values

    Returns
    -------
    string
        The validated string value

    '''
    try:
        out = a2u(value)
    except Exception:
        raise SWATOptionError('Could not convert string value to unicode')

    if max_length is not None and len(out) > max_length:
        raise SWATOptionError('%s is longer than the maximum length of %s' %
                              (out, max_length))

    if min_length is not None and len(out) < min_length:
        raise SWATOptionError('%s is shorter than the minimum length of %s' %
                              (out, min_length))

    if pattern is not None and not re.search(pattern, out):
        raise SWATOptionError('%s does not match pattern %s' % (out, pattern))

    if valid_values is not None and out not in valid_values:
        raise SWATOptionError('%s is not one of the possible values: %s' %
                              (out, ', '.join(valid_values)))

    return out
github sassoftware / python-swat / swat / utils / config.py View on Github external
try:
        out = int(value)
    except Exception:
        raise SWATOptionError('Could not convert %s to an integer' % value)

    if minimum is not None:
        if out < minimum:
            raise SWATOptionError('%s is smaller than the minimum value of %s' %
                                  (out, minimum))
        if exclusive_minimum and out == minimum:
            raise SWATOptionError('%s is equal to the exclusive nimum value of %s' %
                                  (out, minimum))

    if maximum is not None:
        if out > maximum:
            raise SWATOptionError('%s is larger than the maximum value of %s' %
                                  (out, maximum))
        if exclusive_maximum and out == maximum:
            raise SWATOptionError('%s is equal to the exclusive maximum value of %s' %
                                  (out, maximum))

    if multiple_of is not None and (out % int(multiple_of)) != 0:
        raise SWATOptionError('%s is not a multiple of %s' % (out, multiple_of))

    return out