Example #1
0
    def validate(self, trace=False):
        ''' Validates the input data. Raises a ValidationError upon failure. '''

        # get row count
        self.rowcount = len(self.raw_data)

        # check for empty cells and mismatched column counts
        column_count = 0 if self.rowcount == 0 else len(self.raw_data[0])
        for row in range(1, self.rowcount):
            count = len(self.raw_data[row])
            if column_count != count:
                raise errors.ValidationError(
                    'Mismatched column counts: {0}, {1}'.format(
                        column_count, count))
            for col in range(count):
                cell = self.raw_data[row][col]
                if len(cell) == 0:
                    raise errors.ValidationError(
                        'Empty cell at ({0},{1})'.format(row, col))

        self.colcount = column_count

        if trace:
            print 'TRACE input matrix has {0} rows and {1} columns'.format(
                self.rowcount, self.colcount)
Example #2
0
def check_input_single(seq):
    """Function for check sequence from input
    if a single siRNA strand have only actgu letters and is 19-21 nucleotides
    long.
    Also rigth end of siRNA is cut if contain 'uu' or 'tt'.

    Args:
        seq(str): sequence

    Returns:
        List of sequence, warning(or None), True

    Raises:
        ValidationError
    """
    seq = seq.upper().replace('U', 'T')
    pattern = re.compile(r'^[ACGT]{19,21}$')
    cut_warn = "cut 'UU' or 'TT'"

    if not pattern.search(seq):
        if len(seq) > 21 or len(seq) < 19:
            raise errors.ValidationError('%s' % errors.len_error)
        raise errors.ValidationError('%s' % errors.patt_error)
    elif seq[-2:] == "TT" and pattern.search(seq):
        seq = seq[:-2]
        logging.warn(cut_warn)
        return [seq, cut_warn, True]
    elif pattern.search(seq):
        return [seq, None, True]
Example #3
0
def get_param(source,
              param_name,
              validator=None,
              required=True,
              default=None,
              setto=None,
              no_null=True):
    """
    A wrapper on a get-able source to allow validators as well.

    Parameters:
        source      -   An interface that provides a get method to obtain a value by key and a check for containment.
        param_name  -   Name of the parameter whose value is to be fetched.
        validator   -   The validator function to be applied (if one exists).
        required    -   Ensures that the value actually exists
                        before validation is applied.
        default     -   If a value does not exist, then a default
                        is used (before validation is applied).
        no_null     -   If True then a ValidationError is raised
                        if the validator function returns a None
                        value (even if it does not raise an exception).
    """
    provided = param_name in source
    if required and default is None and not provided:
        raise errors.ValidationError(
            "Param '%s' required but not provided and default is None." %
            param_name)

    if not provided:
        value = default
    else:
        value = source.get(param_name)

    # Validate the value if it is required *or* provided
    if required or provided:
        if validator:
            if type(validator) is list:
                for v in validator:
                    value = v(value)
            else:
                value = validator(value)
        if value is None and no_null:
            raise errors.ValidationError(
                "Parameter '%s' was found but validation return a null value."
                % param_name)

        if setto is not None:
            target, attr = setto
            setattr(target, attr, value)
    return value
Example #4
0
    def convert(self, trace=False):
        ''' Converts entries to numerical values. Handles an indicator for missing values. '''

        try:
            for i in range(self.rowcount):
                row = self.raw_data[i]
                row_converted = []
                for j in range(len(row)):
                    value = self.raw_data[i][j]

                    # record missing values as 'nan'
                    if self.missing_designator and value == self.missing_designator:
                        if trace:
                            print 'TRACE missing value \'{0}\'found at ({1},{2})'.format(
                                value, i, j)
                        row_converted.append(float('nan'))
                    else:
                        row_converted.append(float(value))

                self.converted.append(row_converted)

        except ValueError as e:
            raise errors.ValidationError(
                'Non-numerical value at {0}, {1}'.format(i, j))

        if trace: print 'TRACE converted data: ', self.converted
Example #5
0
 def ensurer(strinput):
     try:
         if type(strinput) is datetime.datetime:
             return strinput
         else:
             return datetime.datetime.strptime(strinput, date_format)
     except ValueError, ve:
         raise errors.ValidationError(ve.message)
Example #6
0
def validate(data, validate_class):
    validate_data = validate_class(data)
    try:
        validate_data.validate()
    except schematics.exceptions.ValidationError as e:
        raise errors.ValidationError(400, error_messages=e.messages)

    return validate_data
Example #7
0
def get_required_field(name, verbose_name=None):
    """Retrieve a field or raise an error."""

    try:
        return flask.request.form[name]
    except KeyError:
        verbose_name = verbose_name or name
        raise errors.ValidationError('%s is a required field.' % verbose_name)
Example #8
0
    def validate_body(self, request):
        body = request.bounded_stream.read()

        try:
            request_data = json.loads(body)
        except ValueError:
            self.logger.warning('malformed request body', body=str(body))
            raise errors.ValidationError()
        return request_data
Example #9
0
 def test_validated(self):
     dummy_validator = MagicMock()
     func = validated(dummy_validator)(lambda *a, **k: 'ok')
     self.assertEqual(func(), 'ok')
     # raise error
     dummy_validator.side_effect = errors.ValidationError('a')
     with patch('decorators.send_error') as mock:
         mock.return_value = '_error'
         self.assertEqual(func(), '_error')
         mock.assert_called_once_with('a is not valid')
Example #10
0
 def get_public_url(self, file_id, file_key):
     '''
     Get a files public link including decrypted key
     '''
     if file_id and file_key:
         public_handle = self.api_request({'a': 'l', 'n': file_id})
         decrypted_key = a32_to_base64(file_key)
         return '{0}://{1}/#!%s!%s'.format(self.schema, self.domain) % (public_handle, decrypted_key)
     else:
         raise errors.ValidationError('File id and key must be set')
Example #11
0
def parse_month(month):
    if month == 'all':
        return 0

    try:
        return int(month)
    except ValueError:
        try:
            return constants.MONTHS_MAP[month.title()]
        except KeyError:
            raise errors.ValidationError('Invalid "Month" input.')
Example #12
0
def parse_day(day):
    if day == 'all':
        return 0

    try:
        return int(day)
    except ValueError:
        try:
            return constants.DAYS_MAP[day.title()]
        except KeyError:
            raise errors.ValidationError('Invalid "Day" input.')
Example #13
0
 def __init__(self, request):
     self.form, self.cleaned_data, self._error = {}, {}, None
     for name, params in self.fields.items():
         value = get_request_arg(request, name)
         if params.get('required') and value is None:
             raise errors.ValidationRequiredError(name)
         if value is not None:
             try:
                 value = get_argument(value, params['type'])
             except (TypeError, ValueError):
                 raise errors.ValidationError(name)
         else:
             value = params.get('default')
         self.form[name] = value
Example #14
0
 def get_link(self, file):
     '''
     Get a file public link from given file object
     '''
     #print "file=",file
     file = file[1]
     if 'h' in file and 'k' in file:
         public_handle = self.api_request({'a': 'l', 'n': file['h']})
         file_key = file['k'][file['k'].index(':') + 1:]
         decrypted_key = a32_to_base64(
             decrypt_key(base64_to_a32(file_key), self.master_key))
         return '{0}://{1}/#!{2}!{3}'.format(self.schema, self.domain,
                                             public_handle, decrypted_key)
     else:
         raise errors.ValidationError('File id and key must be present')
Example #15
0
def check_complementary(seq1, seq2):
    """Test for complementary, if both strands are in 5'-3' orientation
    class perform only when there are two strands given; should take as input
    both strand,
    input:
    * 5'acggcttGGaactuctggtac3'
    * 5'gtaccagaagttccaagccgt3'
    reverse second:
    * 3'tgccgaaccttgaagaccatg5'
    translate second strand in a way (a->t, t->a, u->a, c->g, g->c),
    * 5'acggcttGGaactuctggtac3'
    check if the strands are the same,
    starting with first nucleotide or -2,-1,
    +1,+2 (from the beggining or the end) with minimum 80% similarity

    3) 5'acggcttGGaactuctggtac3'
         |||||||||||||||||||||
       3'tgccgaaccttgaagaccatg5'
       5'acggcttGGaactuctggtac3'

    output: 'first sequence' (19-21nt), 'second sequence' (19-21nt), left_end
    {-4,-3,-2,-1,0,1,2,3,4}, rigth_end{-4,-3,-2,-1,0,1,2,3,4}

    Args:
        seq1(str): frist sequence
        seq2(str): second sequence

    Returns:
        tuple (first sequence(str), second sequence(str),
               left offset(int), right offset(int))
    """
    nr_offset = 5
    tab = []
    end_offset = len(seq1) - len(seq2)
    if check_complementary_single(seq1, seq2) >= 80:
        tab.append((seq1, seq2, 0, end_offset))

    for offset in range(1, nr_offset):
        if check_complementary_single(seq1[offset:], seq2) >= 80:
            end_offset = len(seq1) - len(seq2) - offset
            tab.append((seq1, seq2, offset, end_offset))
        if check_complementary_single(seq1, seq2[:-offset]) >= 80:
            end_offset = len(seq1) - len(seq2) + offset
            tab.append((seq1, seq2, -offset, end_offset))
    if not tab:
        raise errors.ValidationError(errors.error)
    return tab[0]
Example #16
0
def check_input(seq_to_be_check):
    """Function for checking many sequences and throw error if wrong input
    input limitations: possible letters: {ACTGUactgu}, change all 'u' to 't',
    length 19-21, one strand or two strands splitted by space,
    if two strands check if they are in correct 5'-3' orientation, allow |_20%_|
    mismatches,
    if the sequence is correct input returns 'first sequence' (19-21nt), 'second
    sequence' (19-21nt), left_end{-4,-3,-2,-1,0,1,2,3,4},
    rigth_end{-4,-3,-2,-1,0,1,2,3,4}
    messages:
    * "correct sequence"
    * "changed 'u' to 't'"
    * "cut 'uu' or 'tt' ends"
    errors:
    * "too short"
    * "insert your siRNA sequence"
    * "too long"
    * "insert only one siRNA sequence or both strands of one siRNA at a time;
    check if both stands are in 5'-3' orientation"
    * "sequence can contain only {actgu} letters

    Args:
        seq_to_be_check: sequence(str) which will be check

    Returns:
        tuple from check_complementary

    Raises:
        ValidationError
    """
    sequence = seq_to_be_check.split(" ")
    len_seq = len(sequence)
    if len_seq == 1:
        return (check_input_single(sequence[0])[0], '', 0, 0)
    elif len_seq == 2:
        ch_seq1 = check_input_single(sequence[0])
        ch_seq2 = check_input_single(sequence[1])
        if ch_seq1[2] and ch_seq2[2]:
            return check_complementary(ch_seq1[0], ch_seq2[0])
    else:
        raise errors.ValidationError('{}'.format(errors.error))
Example #17
0
	def _find_suitable_storage_targets(self, media_id, data, distribution_total):
		"""
		Find distribution_total nodes capable of storing the supplied binary data.

		@param media_id: Unique id for the media.
		@type media_id: String

		@param data: Binary data to store
		@type data: String

		@param distribution_total: Number of nodes to get
		@type distribution_total: Integer

		@return: Hosts
		@rtype: List
		"""
		try:
			media_id = validation.media_id(media_id)
			if md5(data).hexdigest() != media_id:
				raise errors.ValidationError("media_id doesn't match data!")
		except errors.ValidationError, ex:
			return utils.return_deferred_error(ex.value)
Example #18
0
def main():
    global CITY_CHOICE

    while True:
        try:
            CITY_CHOICE, month, day = get_filters()
            df = load_data(CITY_CHOICE, month, day)

            if len(df) == 0:
                raise errors.ValidationError('No data to be displayed. Please change your filter options.')

            time_stats(df)
            station_stats(df)
            trip_duration_stats(df)
            user_stats(df)
        except errors.ValidationError as e:
            print('=' * 40)
            print('Error: {}'.format(e.message))
            print('=' * 40)
        finally:
            restart = input('\nWould you like to restart? Enter yes or no.\n')
            if restart.lower() != 'yes':
                break
Example #19
0
def datetime_from_now(delta_in_seconds):
    if type(delta_in_seconds) not in (float, long, int):
        raise errors.ValidationError("Expected long/float, found: %s" %
                                     str(type(delta_in_seconds)))

    return datetime.datetime.utcnow() + datetime.timedelta(0, delta_in_seconds)
Example #20
0
 def decorator(self, *args, **kwargs):
     val = validator(request)
     if not val.is_valid():
         raise errors.ValidationError(val.get_error())
     self.data = val.cleaned_data
     return method(self, *args, **kwargs)
Example #21
0
def parse_city(city):
    city = city.strip().replace('_', ' ').title()
    if city not in constants.CITY_DATA:
        raise errors.ValidationError('Invalid "City" input.')

    return city
Example #22
0
 def _validateKeypress(self, keys):
     """Validates a keypress list"""
     for key in keys:
         if key.upper() not in values.key_list:
             raise errors.ValidationError("Invalid Key: {}".format(key))
     return True
Example #23
0
def parse_filter_choice(filter_choice):
    filter_choice = filter_choice.strip().lower()
    if filter_choice not in constants.ALLOWED_FILTER_CHOICES:
        raise errors.ValidationError('Invalid "Filter Choice" input.')

    return filter_choice