Repository: sql-machine-learning/elasticdl

Issue:
[Flake8] Part 3 out of 6
```
elasticdl/python/elasticdl/master/servicer.py:12:1: E402 module level import not at top of file
elasticdl/python/elasticdl/master/servicer.py:14:1: E402 module level import not at top of file
elasticdl/python/elasticdl/master/servicer.py:15:1: E402 module level import not at top of file
elasticdl/python/elasticdl/master/servicer.py:16:1: E402 module level import not at top of file
elasticdl/python/elasticdl/master/servicer.py:16:80: E501 line too long (90 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:17:1: E402 module level import not at top of file
elasticdl/python/elasticdl/master/servicer.py:17:80: E501 line too long (109 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:102:80: E501 line too long (84 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:140:80: E501 line too long (83 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:156:80: E501 line too long (83 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:189:80: E501 line too long (90 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:192:21: E722 do not use bare 'except'
elasticdl/python/elasticdl/master/servicer.py:194:80: E501 line too long (103 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:212:80: E501 line too long (83 > 79 characters)
elasticdl/python/elasticdl/master/servicer.py:230:80: E501 line too long (84 > 79 characters)
elasticdl/python/tests/example_test.py:5:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:6:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:7:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:8:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:9:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:11:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:12:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:13:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:14:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:15:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:16:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:17:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:18:1: E402 module level import not at top of file
elasticdl/python/tests/example_test.py:19:1: E402 module level import not at top of file
elasticdl/python/data/codec/tf_example_codec.py:1:1: F401 'enum.Enum' imported but unused
elasticdl/python/data/codec/bytes_codec.py:15:80: E501 line too long (97 > 79 characters)
elasticdl/python/data/codec/bytes_codec.py:25:80: E501 line too long (104 > 79 characters)
elasticdl/python/data/codec/bytes_codec.py:26:21: E122 continuation line missing indentation or outdented
elasticdl/python/data/codec/bytes_codec.py:26:80: E501 line too long (85 > 79 characters)
```


Discussion:

Resolving patch:
diff --git a/elasticdl/python/data/codec/bytes_codec.py b/elasticdl/python/data/codec/bytes_codec.py
--- a/elasticdl/python/data/codec/bytes_codec.py
+++ b/elasticdl/python/data/codec/bytes_codec.py
@@ -12,7 +12,10 @@ def encode(self, data):
         # Check that all columns are present.
         if data.keys() != self._col_id.keys():
             raise ValueError(
-                "Column keys mismatch: expected %s, got %s " % (self._col_id.keys(), data.keys())
+                "Column keys mismatch: expected %s, got %s " % (
+                    self._col_id.keys(),
+                    data.keys()
+                )
             )
 
         # Rearrange the data in order of the columns.
@@ -22,8 +25,13 @@ def encode(self, data):
             column = self._feature_columns[col_id]
             if column.dtype != f_value.dtype or column.shape != f_value.shape:
                 raise ValueError(
-                    "Input data doesn't match column %s definition: column: (%s, %s) data: (%s, %s)" % (
-                    f_name, column.dtype, column.shape, f_value.dtype, f_value.shape)
+                    "Input data doesn't match column %s definition: column: "
+                    "(%s, %s) data: (%s, %s)" % (
+                        f_name,
+                        column.dtype,
+                        column.shape,
+                        f_value.dtype,
+                        f_value.shape)
                 )
             values[col_id] = f_value.tobytes()
         return b"".join(values)
diff --git a/elasticdl/python/data/codec/tf_example_codec.py b/elasticdl/python/data/codec/tf_example_codec.py
--- a/elasticdl/python/data/codec/tf_example_codec.py
+++ b/elasticdl/python/data/codec/tf_example_codec.py
@@ -1,4 +1,3 @@
-from enum import Enum
 import tensorflow as tf
 
 
diff --git a/elasticdl/python/elasticdl/master/servicer.py b/elasticdl/python/elasticdl/master/servicer.py
--- a/elasticdl/python/elasticdl/master/servicer.py
+++ b/elasticdl/python/elasticdl/master/servicer.py
@@ -3,19 +3,23 @@
 import threading
 from collections import defaultdict
 
+from google.protobuf import empty_pb2
+
+from elasticdl.proto import elasticdl_pb2
+from elasticdl.proto import elasticdl_pb2_grpc
+from elasticdl.python.elasticdl.common.ndarray import (
+    ndarray_to_tensor, tensor_to_ndarray
+)
+from elasticdl.python.elasticdl.common.model_helper import (
+    save_checkpoint_to_file, load_from_checkpoint_file
+)
+
 import numpy as np
 
 import tensorflow as tf
 
 assert tf.executing_eagerly()
 
-from google.protobuf import empty_pb2
-
-from elasticdl.proto import elasticdl_pb2
-from elasticdl.proto import elasticdl_pb2_grpc
-from elasticdl.python.elasticdl.common.ndarray import ndarray_to_tensor, tensor_to_ndarray
-from elasticdl.python.elasticdl.common.model_helper import save_checkpoint_to_file, load_from_checkpoint_file
-
 
 class MasterServicer(elasticdl_pb2_grpc.MasterServicer):
     """Master service implementation"""
@@ -99,7 +103,8 @@ def _update_model_version(self):
         self._version += 1
 
     def save_checkpoint(self):
-        file_name = "{}/model_v{}.chkpt".format(self._checkpoint_dir, self._version)
+        file_name = "{}/model_v{}.chkpt".format(
+            self._checkpoint_dir, self._version)
         pb_model = self._get_model_no_lock()
         save_checkpoint_to_file(pb_model, file_name)
         if self._keep_checkpoint_max:
@@ -137,10 +142,9 @@ def _get_model_no_lock(self):
 
     def _validate_model_version(self, request_model_version):
         if request_model_version > self._version:
-            err_msg = "Model version %d not available yet, current version: %d" % (
-                request_model_version,
-                self._version,
-            )
+            err_msg = "Model version %d not available yet, " \
+                      "current version: %d" % (request_model_version,
+                                               self._version)
             self._logger.warning(err_msg)
             raise ValueError(err_msg)
 
@@ -153,7 +157,9 @@ def _validate_model_version(self, request_model_version):
         return invalid_model_version
 
     def ReportGradient(self, request, _):
-        invalid_model_version = self._validate_model_version(request.model_version)
+        invalid_model_version = self._validate_model_version(
+            request.model_version
+        )
 
         res = elasticdl_pb2.ReportGradientResponse()
         if invalid_model_version:
@@ -186,12 +192,15 @@ def ReportGradient(self, request, _):
             self._grad_n += 1
             if self._grad_n >= self._grad_to_wait:
                 self._update_model()
-                if self._checkpoint_steps and self._version % self._checkpoint_steps == 0:
+                if self._checkpoint_steps and (
+                        self._version % self._checkpoint_steps == 0
+                ):
                     try:
                         self.save_checkpoint()
-                    except:
+                    except Exception:
                         self._logger.warning(
-                            "Failed to save checkpoint file for model version {}".format(self._version)
+                            "Failed to save checkpoint file for "
+                            "model version {}".format(self._version)
                         )
 
         res.accepted = True
@@ -209,7 +218,9 @@ def ReportTaskResult(self, request, _):
         return empty_pb2.Empty()
 
     def ReportEvaluationMetrics(self, request, _):
-        invalid_model_version = self._validate_model_version(request.model_version)
+        invalid_model_version = self._validate_model_version(
+            request.model_version
+        )
 
         res = elasticdl_pb2.ReportEvaluationMetricsResponse()
         if invalid_model_version:
@@ -227,7 +238,9 @@ def ReportEvaluationMetrics(self, request, _):
             evaluation_metrics_summary = {
                 k: np.mean(v) for k, v in self._evaluation_metrics.items()
             }
-            self._logger.info("Evaluation metrics: %s" % evaluation_metrics_summary)
+            self._logger.info(
+                "Evaluation metrics: %s" % evaluation_metrics_summary
+            )
         res.accepted = True
         res.model_version = self._version
         return res


Test patch:
diff --git a/elasticdl/python/tests/example_test.py b/elasticdl/python/tests/example_test.py
--- a/elasticdl/python/tests/example_test.py
+++ b/elasticdl/python/tests/example_test.py
@@ -1,7 +1,3 @@
-import tensorflow as tf
-
-tf.enable_eager_execution()
-
 import tempfile
 import os
 import unittest
@@ -18,6 +14,10 @@
 from elasticdl.python.data.codec import TFExampleCodec
 from elasticdl.python.tests.in_process_master import InProcessMaster
 
+import tensorflow as tf
+
+tf.enable_eager_execution()
+
 
 def _get_model_info(file_name):
     module_file = os.path.join(