diff --git a/deepomatic/api/client.py b/deepomatic/api/client.py index a9e699d..d4d181a 100644 --- a/deepomatic/api/client.py +++ b/deepomatic/api/client.py @@ -64,7 +64,7 @@ def __init__(self, *args, **kwargs): More details in the `requests` documentation: https://2.python-requests.org/en/master/user/advanced/#timeouts :type requests_timeout: float or tuple(float, float) :param http_retry (optional): Customize the retry of http errors. - Defaults to `HTTPRetry()`. Check out `http_retry.HTTPRetry` documentation for more information about the parameters and default values. + Defaults to `HTTPRetry()`. See `http_retry.HTTPRetry` documentation for details about parameters and default values. If `None`, no retry will be done on errors. :type http_retry: http_retry.HTTPRetry diff --git a/deepomatic/api/http_helper.py b/deepomatic/api/http_helper.py index 817e6bf..7508d0e 100644 --- a/deepomatic/api/http_helper.py +++ b/deepomatic/api/http_helper.py @@ -76,7 +76,8 @@ def __init__(self, app_id=None, api_key=None, verify_ssl=None, if api_key is None: api_key = os.getenv('DEEPOMATIC_API_KEY') if app_id is None or api_key is None: - raise DeepomaticException("Please specify 'app_id' and 'api_key' either by passing those values to the client or by defining the DEEPOMATIC_APP_ID and DEEPOMATIC_API_KEY environment variables.") + raise DeepomaticException("Please specify 'app_id' and 'api_key' either by passing those values to the client" + " or by defining the DEEPOMATIC_APP_ID and DEEPOMATIC_API_KEY environment variables.") if not isinstance(version, string_types): version = 'v%g' % version diff --git a/deepomatic/api/http_retry.py b/deepomatic/api/http_retry.py index a9c7ded..eeacb30 100644 --- a/deepomatic/api/http_retry.py +++ b/deepomatic/api/http_retry.py @@ -1,5 +1,3 @@ -import functools - from deepomatic.api import utils from deepomatic.api.exceptions import HTTPRetryError from requests.exceptions import (ProxyError, RequestException, @@ -12,8 +10,8 @@ class retry_if_exception_type(retry_if_exception): # Taken from https://github.com/jd/tenacity/blob/2775f13b34b3ec67a774061a77fcd4e1e9b4157c/tenacity/retry.py#L72 # Extented to support blacklist types def __predicate(self, e): - return (isinstance(e, self.exception_types) and - not isinstance(e, self.exception_types_blacklist)) + return (isinstance(e, self.exception_types) + and not isinstance(e, self.exception_types_blacklist)) def __init__(self, exception_types=Exception, exception_types_blacklist=()): @@ -46,7 +44,9 @@ class HTTPRetry(object): wait_fixed(0.1) + random_wait) ``` :type wait: tenacity.wait_base - :param stop (optional). Tell when to stop retrying. By default it stops retrying after a delay of 60 seconds. A last retry can be done just before this delay is reached, thus the total amount of elapsed time might be a bit higher. More details in tenacity source code https://github.com/jd/tenacity/blob/5.1.1/tenacity/stop.py + :param stop (optional). Tell when to stop retrying. By default it stops retrying after a delay of 60 seconds. + A last retry can be done just before this delay is reached, thus the total amount of elapsed time might be a bit higher. + More details in tenacity source code https://github.com/jd/tenacity/blob/5.1.1/tenacity/stop.py Raises tenacity.RetryError when timeout is reached. :type timeout: tenacity.stop_base """ @@ -69,9 +69,9 @@ def __init__(self, retry_if=None, wait=None, stop=None): if self.retry_if is None: self.retry_status_code = set(HTTPRetry.Default.RETRY_STATUS_CODES) - self.retry_if = (retry_if_result(self.retry_if_status_code) | - retry_if_exception_type(HTTPRetry.Default.RETRY_EXCEPTION_TYPES, - HTTPRetry.Default.RETRY_EXCEPTION_TYPES_BLACKLIST)) + self.retry_if = (retry_if_result(self.retry_if_status_code) + | retry_if_exception_type(HTTPRetry.Default.RETRY_EXCEPTION_TYPES, + HTTPRetry.Default.RETRY_EXCEPTION_TYPES_BLACKLIST)) if self.wait is None: random_wait = wait_random_exponential(multiplier=HTTPRetry.Default.RETRY_EXP_MULTIPLIER, diff --git a/deepomatic/api/inputs.py b/deepomatic/api/inputs.py index 0aaa7bf..d4e7216 100644 --- a/deepomatic/api/inputs.py +++ b/deepomatic/api/inputs.py @@ -60,7 +60,8 @@ def __init__(self, source, encoding=None): is_file = hasattr(source, 'read') is_raw = False if not is_file: - is_raw = (sys.version_info >= (3, 0) and isinstance(source, bytes)) or not any([source.startswith(p) for p in self.supported_protocols]) + is_raw = ((sys.version_info >= (3, 0) and isinstance(source, bytes)) + or not any([source.startswith(p) for p in self.supported_protocols])) if is_raw: if encoding is None: raise DeepomaticException("You must specify 'encoding' when passing raw data") diff --git a/deepomatic/api/resource.py b/deepomatic/api/resource.py index 32405d5..ea865d7 100644 --- a/deepomatic/api/resource.py +++ b/deepomatic/api/resource.py @@ -108,6 +108,7 @@ class ResourceList(Resource): """ This is an helper to access a resource list. """ + def __init__(self, resource_class, helper, uri, offset=None, limit=None, **kwargs): params = copy.deepcopy(kwargs) if offset is not None: diff --git a/deepomatic/api/resources/network.py b/deepomatic/api/resources/network.py index 10c2be9..7209838 100644 --- a/deepomatic/api/resources/network.py +++ b/deepomatic/api/resources/network.py @@ -45,10 +45,10 @@ class Network(ListableResource, This is an helper to manipulate a 'Network' object. """ object_template = { - 'name': RequiredArg(), - 'description': OptionnalArg(), - 'metadata': OptionnalArg(), - 'framework': ImmutableArg(), + 'name': RequiredArg(), + 'description': OptionnalArg(), + 'metadata': OptionnalArg(), + 'framework': ImmutableArg(), 'preprocessing': ImmutableArg(), 'postprocessings': OptionnalArg(mutable=False), } diff --git a/deepomatic/api/resources/recognition.py b/deepomatic/api/resources/recognition.py index d46b212..35005a0 100644 --- a/deepomatic/api/resources/recognition.py +++ b/deepomatic/api/resources/recognition.py @@ -42,10 +42,10 @@ class RecognitionSpec(ListableResource, This is an helper to manipulate a 'Recognition Specification' object. """ object_template = { - 'name': RequiredArg(), + 'name': RequiredArg(), 'description': OptionnalArg(), - 'metadata': OptionnalArg(), - 'outputs': ImmutableArg(), + 'metadata': OptionnalArg(), + 'outputs': ImmutableArg(), 'current_version_id': UpdateOnlyArg() } @@ -72,7 +72,7 @@ class RecognitionVersion(CreateableResource, base_uri = '/recognition/versions/' object_template = { - 'spec_id': RequiredArg(), - 'network_id': RequiredArg(), + 'spec_id': RequiredArg(), + 'network_id': RequiredArg(), 'post_processings': RequiredArg(), } diff --git a/deepomatic/api/resources/task.py b/deepomatic/api/resources/task.py index 2112794..f4021a3 100644 --- a/deepomatic/api/resources/task.py +++ b/deepomatic/api/resources/task.py @@ -85,8 +85,8 @@ def wait(self, **retry_kwargs): """ try: retry_get_tasks(self._refresh_status, - retry_if_exception_type(HTTPRetryError) | - retry_if_result(is_pending_status), + retry_if_exception_type(HTTPRetryError) + | retry_if_result(is_pending_status), **retry_kwargs) except TaskRetryError as retry_error: raise TaskTimeout(self._data, retry_error) diff --git a/demo.py b/demo.py index 80bd644..7633cfe 100644 --- a/demo.py +++ b/demo.py @@ -280,7 +280,9 @@ def demo(client=None): logger.info(result) print_header("Run inference on specific version with a bounding box") - result = version.inference(inputs=[ImageInput(demo_url, bbox={"xmin": 0.1, "ymin": 0.1, "xmax": 0.9, "ymax": 0.9})], show_discarded=True, max_predictions=3) + result = version.inference(inputs=[ImageInput(demo_url, bbox={"xmin": 0.1, "ymin": 0.1, "xmax": 0.9, "ymax": 0.9})], + show_discarded=True, + max_predictions=3) logger.info(result) """ diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 0aba4b5..ddd5e64 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -4,6 +4,8 @@ FROM ${BASE_IMAGE} as builder WORKDIR /app COPY . . +# lint check +RUN flake8 --statistics --verbose RUN python setup.py bdist_wheel diff --git a/requirements.txt b/requirements.txt index 9e16833..01fca15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ promise>=2.1,<3 six>=1.10.0,<2 requests>=2.19.0,<3 # will not work below in python3 tenacity>=5.1,<6 +flake8>=3.7,<4 diff --git a/setup.cfg b/setup.cfg index ed8a958..9f1521c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,3 +3,12 @@ universal = 1 [metadata] license_file = LICENSE + +[flake8] +max-line-length = 140 +ignore = W503 +exclude = + .git, + __pycache__, + build, + dist diff --git a/tests/test_client.py b/tests/test_client.py index 7931691..5b883d7 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -75,8 +75,8 @@ def custom_network(client): model_file = os.path.join(extract_dir, model_file_name) mean_file = os.path.join(extract_dir, mean_file_name) - variables_file = os.path.join(extract_dir+'/variables/', variables_file_name) - variables_data_file = os.path.join(extract_dir+'/variables/', variables_data_file_name) + variables_file = os.path.join(extract_dir + '/variables/', variables_file_name) + variables_data_file = os.path.join(extract_dir + '/variables/', variables_data_file_name) if not os.path.exists(model_file): with zipfile.ZipFile(net_zip) as f: @@ -170,7 +170,8 @@ class TestClient(object): def test_headers(self, client): http_helper = client.http_helper session_headers = http_helper.session.headers - assert session_headers['User-Agent'].startswith('{}-tests/{} {}-python-client/{}'.format(__title__, __version__, __title__, __version__)) + assert session_headers['User-Agent'].startswith( + '{}-tests/{} {}-python-client/{}'.format(__title__, __version__, __title__, __version__)) assert 'platform/' in session_headers['User-Agent'] assert 'python/' in session_headers['User-Agent'] assert session_headers['X-APP-ID'] @@ -254,7 +255,9 @@ def test_create_custom_reco_and_infer(self, client, custom_network): result = spec.inference(inputs=[ImageInput(DEMO_URL)], show_discarded=False, max_predictions=3) assert inference_schema(2, 0, 'golden retriever', 0.8) == result - result = version.inference(inputs=[ImageInput(DEMO_URL, bbox={"xmin": 0.1, "ymin": 0.1, "xmax": 0.9, "ymax": 0.9})], show_discarded=True, max_predictions=3) + result = version.inference(inputs=[ImageInput(DEMO_URL, bbox={"xmin": 0.1, "ymin": 0.1, "xmax": 0.9, "ymax": 0.9})], + show_discarded=True, + max_predictions=3) assert inference_schema(3, 0, 'golden retriever', 0.5) == result versions = spec.versions()