Пример #1
0
    def test_is_methods_exceptions(self):
        """Test is_* methods exceptions

        Args:
            self: TestPrecondition

        Returns:
            None

        Raises:
            None
        """
        # is_true
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_true(False, 'Test for boolean failed')

        self.assertTrue('Test for boolean failed' in str(context.exception))

        # is_string
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_string(1, 'Test for string failed')
        self.assertTrue('Test for string failed' in str(context.exception))

        # is_integer
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_integer('not integer', 'Test for integer failed')
        self.assertTrue('Test for integer failed' in str(context.exception))

        # is_positive_integer
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_positive_integer(
                0, 'Test for positive integer failed')
        self.assertTrue(
            'Test for positive integer failed' in str(context.exception))

        # is_non_negative_integer
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_non_negative_integer(
                -1, 'Test for non-negative integer failed')
        self.assertTrue(
            'Test for non-negative integer failed' in str(context.exception))

        # is_array
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_array(0, 'Test for array failed')
        self.assertTrue('Test for array failed' in str(context.exception))

        # is_dict
        with self.assertRaises(PreconditionException) as context:
            Precondition.is_dict([1, 2], 'Test for dict failed')
        self.assertTrue('Test for dict failed' in str(context.exception))
    def get_field_names(self):
        """Get fields names
        Args:

        Returns:
            Field names in the dataset

        Raises:
            None
        """
        Precondition.is_true(isinstance(self.data_array, np.ndarray),
                             'Invalid data_array')
        # find the index of the field_name in data_array
        field_names = self.data_array[0, :].tolist()
        return field_names
    def get_field(self, field_name, field_dtype=np.str_):
        """Get a field from the data_array and convert to specified data_type
        Args:
            field_name: name of the field to be returned
            field_dtype: datatype to be used

        Returns:
            An NP array of field values for the given field name

        Raises:
            None
        """
        Precondition.is_true(isinstance(self.data_array, np.ndarray),
                             'Invalid data_array')
        # find the index of the field_name in data_array
        field_list = self.get_field_names()
        field_index = field_list.index(field_name)
        field_data = self.data_array[1:, field_index]
        return field_data.astype(field_dtype)
Пример #4
0
    def test_is_methods_go_right(self):
        """Test is_* methods

        Args:
            self: TestPrecondition

        Returns:
            None

        Raises:
            None
        """
        Precondition.is_true(True, 'Test for boolean failed')
        Precondition.is_string('Test', 'Test for string failed')
        Precondition.is_integer(1, 'Test for integer failed')
        Precondition.is_positive_integer(1, 'Test for positive integer failed')
        Precondition.is_non_negative_integer(
            0, 'Test for positive integer failed')
        Precondition.is_array([1, 3], 'Test for array failed')
        Precondition.is_dict({'a': 'test'}, 'Test for message failed')
Пример #5
0
    def __init__(self, log_file_path, logger_impl, logger_name=None):
        """Init

        Args:
            log_file_path: absolute path to the log file
            logger_impl: logger implementation object
            logger_name: name of the log file

        Returns:
            None

        Raises:
            None
        """
        Precondition.is_string(log_file_path, 'Invalid log_file_path')
        Precondition.is_true(logger_impl, 'Invalid logger_impl')

        self.log_file_path = log_file_path
        self.log_file_name = logger_name
        self.logger_impl = logger_impl