Ejemplo n.º 1
0
    def test_external_file_save_by_env(self):
        target_resource = os.getenv('TEST_SAVE_URL')

        if target_resource:
            result_path = utils.save_file(self._temp_file_path,
                                          target_resource)
            self.assertEqual(target_resource, result_path)
Ejemplo n.º 2
0
    def test_external_file_save_local(self):
        target_file_path = os.path.join(self._work_directory, 't2')

        self.assertTrue(os.path.exists(self._temp_file_path))
        self.assertFalse(os.path.exists(target_file_path))

        result_path = utils.save_file(self._temp_file_path, target_file_path)

        self.assertEqual(result_path, target_file_path)
        self.assertTrue(os.path.exists(self._temp_file_path))
        self.assertTrue(os.path.exists(target_file_path))
Ejemplo n.º 3
0
    def test_external_file_save_http_bin(self):
        httpbin_image = "kennethreitz/httpbin"
        LOGGER.info('Starting httpbin container from {} image...'.format(
            httpbin_image))

        with LegionTestContainer(image=httpbin_image,
                                 port=80) as httpbin_container:
            target_resource = 'http://localhost:{}/put'.format(
                httpbin_container.host_port)
            result_path = utils.save_file(self._temp_file_path,
                                          target_resource)

            self.assertEqual(target_resource, result_path)
Ejemplo n.º 4
0
def export(filename=None,
           apply_func=None,
           prepare_func=None,
           param_types=None,
           input_data_frame=None,
           version=None,
           use_df=True):
    """
    Export simple Pandas based model as a bundle

    :param filename: the location to write down the model
    :type filename: str
    :param apply_func: an apply function DF->DF
    :type apply_func: func(x) -> y
    :param prepare_func: a function to prepare input DF->DF
    :type prepare_func: func(x) -> y
    :param param_types: result of deduce_param_types
    :type param_types: dict[str, :py:class:`legion.model.types.ColumnInformation`]
    :param input_data_frame: pandas DF
    :type input_data_frame: :py:class:`pandas.DataFrame`
    :param use_df: use pandas DF for prepare and apply function
    :type use_df: bool
    :param version: of version
    :type version: str
    :return: :py:class:`legion.model.ScipyModel` -- model instance
    """
    if prepare_func is None:

        def prepare_func(input_dict):
            """
            Return input value (default prepare function)
            :param x: dict of values
            :return: dict of values
            """
            return input_dict

    column_types = None

    if param_types is not None and input_data_frame is not None:
        raise Exception(
            'You cannot provide param_types and input_data_frame in one time')

    if param_types is None and input_data_frame is None:
        raise Exception('You should provide param_types or input_data_frame')

    if param_types is not None:
        column_types = param_types
    elif input_data_frame is not None:
        column_types = _get_column_types(input_data_frame)

    if not isinstance(column_types, dict) \
            or not column_types.keys() \
            or not isinstance(list(column_types.values())[0], ColumnInformation):
        raise Exception('Bad param_types / input_data_frame provided')

    file_name_has_been_deduced = False
    if filename:
        print(
            'Warning! If you pass filename, CI tools would not work correctly',
            file=sys.stderr)
    else:
        filename = deduce_model_file_name(version)
        file_name_has_been_deduced = True

    model = ScipyModel(apply_func=apply_func,
                       column_types=column_types,
                       prepare_func=prepare_func,
                       version=version,
                       use_df=use_df)

    temp_file = tempfile.mktemp('model-temp')
    with ModelContainer(temp_file, is_write=True) as container:
        container.save(model)

    result_path = save_file(temp_file, filename)

    if file_name_has_been_deduced:
        print('Model has been saved to %s' % result_path, file=sys.stderr)

    send_header_to_stderr(legion.containers.headers.MODEL_PATH, result_path)
    send_header_to_stderr(legion.containers.headers.MODEL_VERSION, version)

    return model
    def test_external_file_save_http_bin(self):
        target_resource = 'https://httpbin.org/put'

        result_path = utils.save_file(self._temp_file_path, target_resource)

        self.assertEqual(target_resource, result_path)