ソースを参照

use numbers.Integral and numbers.Real instead of int and float somewhere

quarrying 3 年 前
コミット
4632f4b99f
3 ファイル変更14 行追加9 行削除
  1. 6 4
      khandy/image/crop_or_pad.py
  2. 5 3
      khandy/image/translate.py
  3. 3 2
      khandy/utils_file_io.py

+ 6 - 4
khandy/image/crop_or_pad.py

@@ -1,3 +1,5 @@
+import numbers
+
 import numpy as np
 
 
@@ -10,8 +12,8 @@ def crop_or_pad(image, x_min, y_min, x_max, y_max, border_value=0):
         tf.image.resize_image_with_crop_or_pad
     """
     assert image.ndim in [2, 3]
-    assert isinstance(x_min, int) and isinstance(y_min, int)
-    assert isinstance(x_max, int) and isinstance(y_max, int)
+    assert isinstance(x_min, numbers.Integral) and isinstance(y_min, numbers.Integral)
+    assert isinstance(x_max, numbers.Integral) and isinstance(y_max, numbers.Integral)
     assert (x_min <= x_max) and (y_min <= y_max)
     
     src_height, src_width = image.shape[:2]
@@ -23,7 +25,7 @@ def crop_or_pad(image, x_min, y_min, x_max, y_max, border_value=0):
     else:
         dst_image_shape = (dst_height, dst_width, channels)
 
-    if isinstance(border_value, (int, float)):
+    if isinstance(border_value, numbers.Real):
         dst_image = np.full(dst_image_shape, border_value, dtype=image.dtype)
     elif isinstance(border_value, tuple):
         assert len(border_value) == channels, \
@@ -59,7 +61,7 @@ def crop_or_pad(image, x_min, y_min, x_max, y_max, border_value=0):
     
 def center_crop(image, dst_width, dst_height):
     assert image.ndim in [2, 3]
-    assert isinstance(dst_width, int) and isinstance(dst_height, int)
+    assert isinstance(dst_width, numbers.Integral) and isinstance(dst_height, numbers.Integral)
     assert (image.shape[0] >= dst_height) and (image.shape[1] >= dst_width)
 
     crop_top = (image.shape[0] - dst_height) // 2

+ 5 - 3
khandy/image/translate.py

@@ -1,3 +1,5 @@
+import numbers
+
 import numpy as np
 
 
@@ -20,12 +22,12 @@ def translate_image(image, x_shift, y_shift, border_value=0):
         crop_or_pad
     """
     assert image.ndim in [2, 3]
-    assert isinstance(x_shift, int)
-    assert isinstance(y_shift, int)
+    assert isinstance(x_shift, numbers.Integral)
+    assert isinstance(y_shift, numbers.Integral)
     image_height, image_width = image.shape[:2]
     channels = 1 if image.ndim == 2 else image.shape[2]
     
-    if isinstance(border_value, (int, float)):
+    if isinstance(border_value, numbers.Real):
         dst_image = np.full_like(image, border_value)
     elif isinstance(border_value, tuple):
         assert len(border_value) == channels, \

+ 3 - 2
khandy/utils_file_io.py

@@ -1,10 +1,11 @@
 import json
+import numbers
 from collections import OrderedDict
 
 
 def load_list(filename, encoding='utf-8', start=0, stop=None):
-    assert isinstance(start, int) and start >= 0
-    assert (stop is None) or (isinstance(stop, int) and stop > start)
+    assert isinstance(start, numbers.Integral) and start >= 0
+    assert (stop is None) or (isinstance(stop, numbers.Integral) and stop > start)
     
     lines = []
     with open(filename, 'r', encoding=encoding) as f: