From be33da1dbdee46c9fecdfcb30389bc42f96ed036 Mon Sep 17 00:00:00 2001 From: Lawrence Hudson Date: Thu, 21 Feb 2019 06:48:18 +0000 Subject: [PATCH] [#49] tidying --- DEVELOPING.md | 2 +- pyzbar/locations.py | 10 +++++----- pyzbar/pyzbar.py | 13 +++++++------ pyzbar/pyzbar_error.py | 3 +++ pyzbar/tests/test_pyzbar.py | 11 +++++------ pyzbar/tests/test_read_zbar.py | 5 +++-- pyzbar/tests/test_zbar_library.py | 3 --- pyzbar/wrapper.py | 7 +------ pyzbar/zbar_library.py | 6 +++--- setup.py | 1 + 10 files changed, 29 insertions(+), 32 deletions(-) diff --git a/DEVELOPING.md b/DEVELOPING.md index 466f5ed..4b9c65c 100644 --- a/DEVELOPING.md +++ b/DEVELOPING.md @@ -68,7 +68,7 @@ pip install wheel ``` mkvirtualenv pypi pip install twine - twine upload -r pypitest dist/* + twine upload -r testpypi dist/* ``` 4. Test the release to TestPyPI diff --git a/pyzbar/locations.py b/pyzbar/locations.py index bfa1bee..6ee14d8 100644 --- a/pyzbar/locations.py +++ b/pyzbar/locations.py @@ -3,7 +3,7 @@ from operator import itemgetter -__all__ = ['bounding_box', 'convex_hull', 'Rect'] +__all__ = ['bounding_box', 'convex_hull', 'Point', 'Rect'] Point = namedtuple('Point', ['x', 'y']) @@ -23,14 +23,14 @@ def bounding_box(locations): x_min, x_max = min(x_values), max(x_values) y_values = list(map(itemgetter(1), locations)) y_min, y_max = min(y_values), max(y_values) - return Rect(x_min, y_min, x_max - x_min, y_max - y_min) + return Rect(x_min, y_min, x_max - x_min, y_max - y_min) def convex_hull(points): """Computes the convex hull of an iterable of (x, y) coordinates. Args: - locations: iterable of (x, y) tuples. + points: iterable of (x, y) tuples. Returns: `list`: instances of `Point` - vertices of the convex hull in @@ -47,9 +47,9 @@ def is_not_clockwise(p0, p1, p2): (p1[1] - p0[1]) * (p2[0] - p0[0]) ) - def go(points): + def go(points_): res = [] - for p in points: + for p in points_: while 1 < len(res) and is_not_clockwise(res[-2], res[-1], p): res.pop() res.append(p) diff --git a/pyzbar/pyzbar.py b/pyzbar/pyzbar.py index 40a80c8..ae44367 100644 --- a/pyzbar/pyzbar.py +++ b/pyzbar/pyzbar.py @@ -14,7 +14,9 @@ zbar_symbol_next, ZBarConfig, ZBarSymbol, EXTERNAL_DEPENDENCIES ) -__all__ = ['decode', 'Point', 'Rect', 'Decoded', 'EXTERNAL_DEPENDENCIES'] +__all__ = [ + 'decode', 'Point', 'Rect', 'Decoded', 'ZBarSymbol', 'EXTERNAL_DEPENDENCIES' +] Decoded = namedtuple('Decoded', ['data', 'type', 'rect', 'polygon']) @@ -146,7 +148,8 @@ def _pixel_data(image): # Check dimensions if 0 != len(pixels) % (width * height): - raise PyZbarError(( + raise PyZbarError( + ( 'Inconsistent dimensions: image data of {0} bytes is not ' 'divisible by (width x height = {1})' ).format(len(pixels), (width * height)) @@ -164,15 +167,13 @@ def _pixel_data(image): return pixels, width, height -def decode(image, symbols=None, scan_locations=False): +def decode(image, symbols=None): """Decodes datamatrix barcodes in `image`. Args: image: `numpy.ndarray`, `PIL.Image` or tuple (pixels, width, height) - symbols (ZBarSymbol): the symbol types to decode; if `None`, uses + symbols: iter(ZBarSymbol) the symbol types to decode; if `None`, uses `zbar`'s default behaviour, which is to decode all symbol types. - scan_locations (bool): If `True`, results will include scan - locations. Returns: :obj:`list` of :obj:`Decoded`: The values decoded from barcodes. diff --git a/pyzbar/pyzbar_error.py b/pyzbar/pyzbar_error.py index a2810d3..3abdf02 100644 --- a/pyzbar/pyzbar_error.py +++ b/pyzbar/pyzbar_error.py @@ -1,2 +1,5 @@ +__all__ = ["PyZbarError"] + + class PyZbarError(Exception): pass diff --git a/pyzbar/tests/test_pyzbar.py b/pyzbar/tests/test_pyzbar.py index c52f3e5..683f7e0 100644 --- a/pyzbar/tests/test_pyzbar.py +++ b/pyzbar/tests/test_pyzbar.py @@ -4,10 +4,10 @@ from pathlib import Path try: - from unittest.mock import call, patch, MagicMock + from unittest.mock import patch except ImportError: # Python 2 - from mock import call, patch + from mock import patch import numpy as np @@ -18,7 +18,6 @@ except ImportError: cv2 = None - from pyzbar.pyzbar import ( decode, Decoded, Rect, ZBarSymbol, EXTERNAL_DEPENDENCIES ) @@ -172,7 +171,7 @@ def test_unsupported_bits_per_pixel(self): data = (list(range(3 * 3 * 2)), 3, 3) self.assertRaisesRegexp( PyZbarError, - 'Unsupported bits-per-pixel \[16\]. Only \[8\] is supported.', + r'Unsupported bits-per-pixel \[16\]. Only \[8\] is supported.', decode, data ) self.assertRaises(PyZbarError, decode, data) @@ -183,8 +182,8 @@ def test_inconsistent_dimensions(self): self.assertRaisesRegexp( PyZbarError, ( - 'Inconsistent dimensions: image data of 10 bytes is not ' - 'divisible by \(width x height = 9\)' + r'Inconsistent dimensions: image data of 10 bytes is not ' + r'divisible by \(width x height = 9\)' ), decode, data ) diff --git a/pyzbar/tests/test_read_zbar.py b/pyzbar/tests/test_read_zbar.py index 7d2ac27..ddc7491 100644 --- a/pyzbar/tests/test_read_zbar.py +++ b/pyzbar/tests/test_read_zbar.py @@ -4,9 +4,10 @@ from pathlib import Path from contextlib import contextmanager -if 2 == sys.version_info[0]: +# TODO Would io.StringIO not work in all cases? +try: from cStringIO import StringIO -else: +except ImportError: from io import StringIO from pyzbar.scripts.read_zbar import main diff --git a/pyzbar/tests/test_zbar_library.py b/pyzbar/tests/test_zbar_library.py index b3a159d..137568c 100644 --- a/pyzbar/tests/test_zbar_library.py +++ b/pyzbar/tests/test_zbar_library.py @@ -1,8 +1,5 @@ -import platform -import sys import unittest -from contextlib import contextmanager from pathlib import Path try: diff --git a/pyzbar/wrapper.py b/pyzbar/wrapper.py index 85f19d4..4867220 100644 --- a/pyzbar/wrapper.py +++ b/pyzbar/wrapper.py @@ -1,15 +1,10 @@ """Low-level wrapper around zbar's interface """ -import platform -import sys - from ctypes import ( - cdll, c_ubyte, c_char_p, c_int, c_uint, c_ulong, c_void_p, Structure, + c_ubyte, c_char_p, c_int, c_uint, c_ulong, c_void_p, Structure, CFUNCTYPE, POINTER ) -from ctypes.util import find_library from enum import IntEnum, unique -from pathlib import Path from . import zbar_library diff --git a/pyzbar/zbar_library.py b/pyzbar/zbar_library.py index 4e5b4e8..b63108c 100644 --- a/pyzbar/zbar_library.py +++ b/pyzbar/zbar_library.py @@ -45,13 +45,13 @@ def load(): # cdll.LoadLibrary() imports DLLs alongside executable fname, dependencies = _windows_fnames() - def load_objects(dir): + def load_objects(directory): # Load dependencies before loading libzbar dll deps = [ - cdll.LoadLibrary(str(dir.joinpath(dep))) + cdll.LoadLibrary(str(directory.joinpath(dep))) for dep in dependencies ] - libzbar = cdll.LoadLibrary(str(dir.joinpath(fname))) + libzbar = cdll.LoadLibrary(str(directory.joinpath(fname))) return deps, libzbar try: diff --git a/setup.py b/setup.py index ff59740..409dc05 100755 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ def readme(): + # TODO IOError on Python 2.x. FileNotFoundError on Python 3.x. try: with open('README.rst') as f: return f.read()