From 74ed97753cd99458b4a3279c7092338922d59844 Mon Sep 17 00:00:00 2001 From: Patrick Kuehn Date: Wed, 16 Oct 2019 11:41:54 +0200 Subject: [PATCH] Refactor for travis ci imports and docstrings --- autofocus/predict/app/app.py | 2 +- autofocus/predict/app/models/File.py | 1 + autofocus/predict/app/models/Predictor.py | 3 +-- autofocus/predict/app/models/ZipArchive.py | 9 +++++---- .../app/requests/PredictRequestValidator.py | 12 +++++------ .../requests/PredictZipRequestValidator.py | 12 +++++------ autofocus/predict/app/requests/Validator.py | 20 +++++++++---------- 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/autofocus/predict/app/app.py b/autofocus/predict/app/app.py index 1415f58..d888cb3 100644 --- a/autofocus/predict/app/app.py +++ b/autofocus/predict/app/app.py @@ -49,7 +49,7 @@ def classify_zip(): file = ZipArchive(request.files["file"], app.config["UPLOAD_FOLDER"]) if not file.hasImages(): - validator.error['file'] = "No image files detected in the zip file." + validator.error["file"] = "No image files detected in the zip file." validator.abort() # Extract files diff --git a/autofocus/predict/app/models/File.py b/autofocus/predict/app/models/File.py index 490c15c..62f6446 100644 --- a/autofocus/predict/app/models/File.py +++ b/autofocus/predict/app/models/File.py @@ -1,4 +1,5 @@ import os + from werkzeug import secure_filename diff --git a/autofocus/predict/app/models/Predictor.py b/autofocus/predict/app/models/Predictor.py index e5d4a42..99a0eef 100644 --- a/autofocus/predict/app/models/Predictor.py +++ b/autofocus/predict/app/models/Predictor.py @@ -35,7 +35,7 @@ def predict_multiple(self, files): Parameters: files: Dict with File objects of image file - + Returns: dict: Dictionary of probabilities for each file in files """ @@ -45,7 +45,6 @@ def predict_multiple(self, files): predictions[key] = self.getProbabilities() return predictions - def getProbabilities(self): """ Return formated Probabilities diff --git a/autofocus/predict/app/models/ZipArchive.py b/autofocus/predict/app/models/ZipArchive.py index 89db1cb..9e85be1 100644 --- a/autofocus/predict/app/models/ZipArchive.py +++ b/autofocus/predict/app/models/ZipArchive.py @@ -1,5 +1,6 @@ import os from zipfile import ZipFile + from .File import File from ..requests.Validator import ALLOWED_IMAGE_FILES from ..utils import allowed_file @@ -46,7 +47,7 @@ def listAllImages(self, extensions=ALLOWED_IMAGE_FILES): Parameters: extensions: Array of allowed image extensions - + Returns: array: Array of filenames matching the extension """ @@ -58,7 +59,7 @@ def hasImages(self, extensions=ALLOWED_IMAGE_FILES): Parameters: extensions: Array of allowed image extensions - + Returns: boolean: True if zip has images """ @@ -74,9 +75,9 @@ def extractAll(self, path=None, members=None): Parameters: path: Path to store files members: Files to extract - + Returns: - array: Array of extracted File objects + array: Array of extracted File objects """ self.zip.extractall(path, members) extractedFiles = {} diff --git a/autofocus/predict/app/requests/PredictRequestValidator.py b/autofocus/predict/app/requests/PredictRequestValidator.py index 7dd4af8..9fdfd97 100644 --- a/autofocus/predict/app/requests/PredictRequestValidator.py +++ b/autofocus/predict/app/requests/PredictRequestValidator.py @@ -3,9 +3,7 @@ class PredictRequestValidator(Validator): - """ - Validate request for endpoint predict - """ + """Validate request for endpoint predict""" def validate(self): """ @@ -18,11 +16,13 @@ def validate(self): """ self.error = {} - file = self.request.files.get('file', None) + file = self.request.files.get("file", None) if not file: - self.error['file'] = "No file given." + self.error["file"] = "No file given." elif not allowed_file(file.filename, ALLOWED_IMAGE_FILES): - self.error['file'] = "File type not allowed. File must be of type {allowed}".format( + self.error[ + "file" + ] = "File type not allowed. File must be of type {allowed}".format( allowed=ALLOWED_IMAGE_FILES ) diff --git a/autofocus/predict/app/requests/PredictZipRequestValidator.py b/autofocus/predict/app/requests/PredictZipRequestValidator.py index 34d600f..a913f8e 100644 --- a/autofocus/predict/app/requests/PredictZipRequestValidator.py +++ b/autofocus/predict/app/requests/PredictZipRequestValidator.py @@ -3,9 +3,7 @@ class PredictZipRequestValidator(Validator): - """ - Validate request for endpoint predict_zip - """ + """Validate request for endpoint predict_zip""" def validate(self): """ @@ -18,11 +16,13 @@ def validate(self): """ self.error = {} - file = self.request.files.get('file', None) + file = self.request.files.get("file", None) if not file: - self.error['file'] = "No file given." + self.error["file"] = "No file given." elif not allowed_file(file.filename, ALLOWED_ZIP_FILES): - self.error['file'] = "File type not allowed. File must be of type {allowed}".format( + self.error[ + "file" + ] = "File type not allowed. File must be of type {allowed}".format( allowed=ALLOWED_ZIP_FILES ) diff --git a/autofocus/predict/app/requests/Validator.py b/autofocus/predict/app/requests/Validator.py index 4695d0d..0381246 100644 --- a/autofocus/predict/app/requests/Validator.py +++ b/autofocus/predict/app/requests/Validator.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod + from flask import abort, jsonify, make_response from flask_api import status @@ -48,15 +49,12 @@ def getError(self): dict: The errors found during validation """ return self.error - + def abort(self): - """ - Abort with errors - """ - abort(make_response( - jsonify( - status=status.HTTP_400_BAD_REQUEST, - error=self.getError() - ), - status.HTTP_400_BAD_REQUEST - )) + """Abort with errors""" + abort( + make_response( + jsonify(status=status.HTTP_400_BAD_REQUEST, error=self.getError()), + status.HTTP_400_BAD_REQUEST, + ) + )