예제 #1
0
 def test_temporal_unit_conversion(self):
     self.assertTrue(units_convertible('hours', 'seconds'))
     self.assertFalse(units_convertible('hours', 'hours since 2000-01-01'))
예제 #2
0
파일: util.py 프로젝트: aodn/data-services
def check_value(name, value, operator, ds, check_type, result_name, check_priority, reasoning=None, skip_check_present=False):
    """
    Help method to compare attribute to value or a variable
    to a value. It also returns a Result object based on whether
    the check is successful or not.

    params:
        name (tuple): variable name and attribute name.
                      For global attribute, only attribute name present.
        value (str): expected value
        operator (int): OPERATOR_EQUAL, OPERATOR_MAX, OPERATOR_MIN
        ds (Dataset): netcdf data file
        check_type (int): CHECK_VARIABLE, CHECK_GLOBAL_ATTRIBUTE,
                          CHECK_VARIABLE_ATTRIBUTE
        result_name: the result name to display
        check_priority (int): the check priority
        reasoning (str): reason string for failed check
        skip_check_present (boolean): flag to allow check only performed
                                     if attribute is present
    return:
        result (Result): result for the check
    """
    result = check_present(name, ds, check_type,
                           result_name,
                           check_priority)

    if result.value:
        result = None
        retrieved_value = None
        passed = True
        reasoning_out = None

        if check_type == CHECK_GLOBAL_ATTRIBUTE:
            retrieved_value = getattr(ds, name[0])
            retrieved_name = name[0]

        if check_type == CHECK_VARIABLE:
            variable = ds.variables.get(name[0], None)
            retrieved_name = name[0]

        if check_type == CHECK_VARIABLE_ATTRIBUTE:
            variable = ds.variables.get(name[0], None)
            retrieved_value = getattr(variable, name[1])
            retrieved_name = '%s:%s' % name

        if operator == OPERATOR_EQUAL:
            if retrieved_value != value:
                passed = False
                reasoning_out = reasoning or \
                                ["Attribute %s should be equal to '%s'" % (retrieved_name, str(value))]

        if operator == OPERATOR_MIN:
            min_value = amin(variable.__array__())

            if not np.isclose(min_value, float(value)):
                passed = False
                reasoning_out = reasoning or \
                                ["Minimum value of %s (%f) does not match attributes (%f)" % \
                                 (retrieved_name, min_value, float(value))]

        if operator == OPERATOR_MAX:
            max_value = amax(variable.__array__())
            if not np.isclose(max_value, float(value)):
                passed = False
                reasoning_out = reasoning or \
                                ["Maximum value of %s (%f) does not match attributes (%f)" % \
                                 (retrieved_name, max_value, float(value))]

        if operator == OPERATOR_DATE_FORMAT:
            try:
                datetime.datetime.strptime(retrieved_value, value)
            except ValueError:
                passed = False
                reasoning_out = reasoning or \
                                ["Attribute %s is not in correct date/time format (%s)" % \
                                 (retrieved_name, value)]

        if operator == OPERATOR_SUB_STRING:
            if value not in retrieved_value:
                passed = False
                reasoning_out = reasoning or \
                                ["Attribute %s should contain the substring '%s'" % \
                                 (retrieved_name, value)]

        if operator == OPERATOR_CONVERTIBLE:
            if not units_convertible(retrieved_value, value):
                passed = False
                reasoning_out = reasoning or \
                                ["Units %s should be equivalent to %s" % (retrieved_name, value)]

        if operator == OPERATOR_EMAIL:
            if not is_valid_email(retrieved_value)[0]:
                passed = False
                reasoning_out = reasoning or ["Attribute %s is not a valid email address" % \
                                              retrieved_name]

        if operator == OPERATOR_WITHIN:
            if retrieved_value not in value:
                passed = False
                reasoning_out = reasoning or ["Attribute %s is not in the expected range (%s)" % \
                                              (retrieved_name, str(value))]

        result = Result(check_priority, passed, result_name, reasoning_out)

    else:
        if skip_check_present:
            result = None

    return result
예제 #3
0
 def test_temporal_unit_conversion(self):
     self.assertTrue(units_convertible('hours', 'seconds'))
     self.assertFalse(units_convertible('hours', 'hours since 2000-01-01'))
예제 #4
0
 def test_temporal_unit_conversion(self):
     self.assertTrue(units_convertible("hours", "seconds"))
     self.assertFalse(units_convertible("hours", "hours since 2000-01-01"))