From f4c3b32f72b348a8a8c9d8ac2518da5fb4dd069c Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez-Martin Date: Thu, 31 Oct 2019 14:26:26 +0000 Subject: [PATCH] Fix linter --- python/seldon_core/wrapper.py | 11 ++-- python/tests/test_model_microservice.py | 72 +++++++++++++++---------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/python/seldon_core/wrapper.py b/python/seldon_core/wrapper.py index 6d8c63a8e2..c8a613e95e 100644 --- a/python/seldon_core/wrapper.py +++ b/python/seldon_core/wrapper.py @@ -3,12 +3,7 @@ from flask import jsonify, Flask, send_from_directory, request from flask_cors import CORS import logging -from seldon_core.utils import ( - json_to_seldon_message, - seldon_message_to_json, - json_to_feedback, - json_to_seldon_messages, -) +from seldon_core.utils import seldon_message_to_json, json_to_feedback from seldon_core.flask_utils import get_request import seldon_core.seldon_methods from seldon_core.flask_utils import ( @@ -49,8 +44,8 @@ def Predict(): logger.debug("REST Request: %s", request) response = seldon_core.seldon_methods.predict(user_model, requestJson) json_response = jsonify(response) - if 'status' in response and 'code' in response['status']: - json_response.status_code = response['status']['code'] + if "status" in response and "code" in response["status"]: + json_response.status_code = response["status"]["code"] logger.debug("REST Response: %s", response) return json_response diff --git a/python/tests/test_model_microservice.py b/python/tests/test_model_microservice.py index 09d22ef344..a1733c5deb 100644 --- a/python/tests/test_model_microservice.py +++ b/python/tests/test_model_microservice.py @@ -12,8 +12,6 @@ from seldon_core.flask_utils import SeldonMicroserviceException from seldon_core.tf_helper import _TF_MISSING -from flask import jsonify - from utils import skipif_tf_missing if not _TF_MISSING: @@ -103,6 +101,7 @@ def send_feedback_rest(self, request): def send_feedback_grpc(self, request): print("Feedback called") + class UserObjectLowLevelWithStatusInResponse(SeldonComponent): def __init__(self, metrics_ok=True, ret_nparray=False): self.metrics_ok = metrics_ok @@ -110,15 +109,15 @@ def __init__(self, metrics_ok=True, ret_nparray=False): self.nparray = np.array([1, 2, 3]) def predict_rest(self, request): - return {"data": {"ndarray": [9, 9]}, "status": {"code": 400, "status": "FAILURE"}} + return { + "data": {"ndarray": [9, 9]}, + "status": {"code": 400, "status": "FAILURE"}, + } def predict_grpc(self, request): arr = np.array([9, 9]) datadef = prediction_pb2.DefaultData( - tensor=prediction_pb2.Tensor( - shape=(2, 1), - values=arr - ) + tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr) ) request = prediction_pb2.SeldonMessage(data=datadef) return request @@ -132,27 +131,33 @@ def send_feedback_grpc(self, request): class UserObjectLowLevelWithStatusInResponseWithPredictRaw(SeldonComponent): def __init__(self, check_name): - self.check_name=check_name + self.check_name = check_name def predict_raw(self, msg): - msg=json_to_seldon_message(msg) - if self.check_name == 'img': - file_data=msg.binData - img = Image.open(io.BytesIO (file_data)) + msg = json_to_seldon_message(msg) + if self.check_name == "img": + file_data = msg.binData + img = Image.open(io.BytesIO(file_data)) img.verify() - return {"meta": seldon_message_to_json(msg.meta), - "data": {"ndarray": [rs232_checksum(file_data).decode('utf-8')]}, - "status": {"code": 400, "status": "FAILURE"}} - elif self.check_name == 'txt': - file_data=msg.binData - return {"meta": seldon_message_to_json(msg.meta), - "data": {"ndarray": [file_data.decode('utf-8')]}, - "status": {"code": 400, "status": "FAILURE"}} - elif self.check_name == 'strData': - file_data=msg.strData - return {"meta": seldon_message_to_json(msg.meta), - "data": {"ndarray": [file_data]}, - "status": {"code": 400, "status": "FAILURE"}} + return { + "meta": seldon_message_to_json(msg.meta), + "data": {"ndarray": [rs232_checksum(file_data).decode("utf-8")]}, + "status": {"code": 400, "status": "FAILURE"}, + } + elif self.check_name == "txt": + file_data = msg.binData + return { + "meta": seldon_message_to_json(msg.meta), + "data": {"ndarray": [file_data.decode("utf-8")]}, + "status": {"code": 400, "status": "FAILURE"}, + } + elif self.check_name == "strData": + file_data = msg.strData + return { + "meta": seldon_message_to_json(msg.meta), + "data": {"ndarray": [file_data]}, + "status": {"code": 400, "status": "FAILURE"}, + } class UserObjectLowLevelWithPredictRaw(SeldonComponent): @@ -310,16 +315,25 @@ def test_model_lowlevel_multi_form_data_strData_ok(): ) - def test_model_lowlevel_multi_form_data_strData_non200status(): - user_object = UserObjectLowLevelWithStatusInResponseWithPredictRaw('strData') + user_object = UserObjectLowLevelWithStatusInResponseWithPredictRaw("strData") app = get_rest_microservice(user_object) client = app.test_client() - rv = client.post('/predict',data={"meta":'{"puid":"1234"}',"strData":(f'./tests/resources/test.txt','test.txt')},content_type='multipart/form-data') + rv = client.post( + "/predict", + data={ + "meta": '{"puid":"1234"}', + "strData": (f"./tests/resources/test.txt", "test.txt"), + }, + content_type="multipart/form-data", + ) j = json.loads(rv.data) assert rv.status_code == 400 assert j["meta"]["puid"] == "1234" - assert j["data"]["ndarray"][0] == "this is test file for testing multipart/form-data input\n" + assert ( + j["data"]["ndarray"][0] + == "this is test file for testing multipart/form-data input\n" + ) def test_model_multi_form_data_ok():