Beispiel #1
0
    def _validate_param(self, data, name):
        value = data.get(name, None)
        if value is None:
            raise ValidationError('Parameter {} is required'.format(name))

        param_type = FEATURE_PARAMS_TYPES[name]['type']
        if param_type == 'int':
            if not isint(value):
                raise ValidationError('{} - int is required'.format(value))

        elif param_type == 'str':
            pass  # do nothing

        elif param_type == 'text':
            if isinstance(value, basestring):
                try:
                    data[name] = json.loads(value)
                except ValueError as e:
                    raise ValidationError('invalid json: {}'.format(value), e)

        elif param_type == 'dict':
            if not isinstance(value, dict):
                raise ValidationError(
                    '{} should be a dictionary'.format(name))
            if not value.keys():
                raise ValidationError(
                    'Map {} should contain at least one value'.format(name))
            for key, val in value.items():
                if not len(str(val)):
                    raise ValidationError(
                        'Value {0} in {1} can\'t be empty'.format(key, name))
        elif param_type == 'list':
            if not isinstance(value, basestring):
                raise ValidationError(
                    '{} should be a list'.format(name))
Beispiel #2
0
    def _get_pig_fields_action(self, **kwargs):
        if 'id' not in kwargs:
            raise ValueError("Specify id of the datasource")

        ds = self._get_details_query({}, **kwargs)
        if ds is None:
            raise NotFound('DataSet not found')

        fields_data = []

        for key, val in ds.pig_row.iteritems():
            if isint(val):
                data_type = 'integer'
            elif isfloat(val):
                data_type = 'float'
            else:
                data_type = 'string'
            fields_data.append({'column_name': key, 'data_type': data_type})

        from ..utils import XML_FIELD_TEMPLATE
        xml = "\r\n".join(
            [XML_FIELD_TEMPLATE % field for field in fields_data])
        return self._render({
            'sample_xml': xml,
            'fields': fields_data,
            'pig_result_line': ds.pig_row,
        })
Beispiel #3
0
def convert_float_or_int(val, config):
    if '.' not in str(val) and isint(val):
        return int(val)
    elif isfloat(val):
        return float(val)
    else:
        raise ValidationError('Invalid value {0} for type {1}'.format(
            val, config.type))
Beispiel #4
0
    def clean(self, value):
        value = super(ModelField, self).clean(value)

        if value is None:
            return None

        if not isint(value):
            raise ValidationError('Invalid {0} id: {1}'.format(
                self.Model.__name__, value))

        self.model = self.Model.query.get(value)
        if self.model is None:
            raise ValidationError('{0} not found'.format(self.Model.__name__))

        return self.model if self.return_model else value
Beispiel #5
0
    def clean(self, value):
        value = super(MultipleModelField, self).clean(value)

        if not value:
            return None

        values = value.split(',')
        for item in values:
            if not isint(item):
                raise ValidationError('Invalid {0} id: {1}'.format(
                    self.Model.__name__, item))

        self.models = self.Model.query.filter(self.Model.id.in_(values)).all()
        if not self.models:
            raise ValidationError('{0} not found'.format(self.Model.__name__))

        return self.models if self.return_model else value
Beispiel #6
0
def convert_int_float_string_none(val, config):
    """
    Parses parameter that could be int, float, string or none.
    A sample of this is max_features in Decision Tree Classifier.
    """
    if not val:
        return None

    if '.' not in str(val) and isint(val):
        return int(val)
    elif isfloat(val):
        return float(val)
    else:  # string
        choices = config.get('choices')
        if choices:
            check_choices(config.get('name'), val, choices)

    return val
Beispiel #7
0
 def _clean_param(self, data, name):
     param_type = FEATURE_PARAMS_TYPES[name]['type']
     value = data.get(name, None)
     if param_type == 'dict':
         new_dict = {}
         for key, val in value.iteritems():
             if isint(val):
                 new_dict[key] = int(val)
             elif isfloat(val):
                 new_dict[key] = float(val)
             else:
                 new_dict[key] = val
         return new_dict
     elif param_type == 'int':
         return int(value)
     elif param_type == 'boolean':
         return bool(value)
     elif param_type == 'list':
         return list(json.loads(value))
     else:
         return value