Skip to content

Commit

Permalink
[#49] tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
quicklizard99 committed Feb 21, 2019
1 parent 66f478a commit be33da1
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pyzbar/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions pyzbar/pyzbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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))
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions pyzbar/pyzbar_error.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
__all__ = ["PyZbarError"]


class PyZbarError(Exception):
pass
11 changes: 5 additions & 6 deletions pyzbar/tests/test_pyzbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,7 +18,6 @@
except ImportError:
cv2 = None


from pyzbar.pyzbar import (
decode, Decoded, Rect, ZBarSymbol, EXTERNAL_DEPENDENCIES
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
)
Expand Down
5 changes: 3 additions & 2 deletions pyzbar/tests/test_read_zbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions pyzbar/tests/test_zbar_library.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import platform
import sys
import unittest

from contextlib import contextmanager
from pathlib import Path

try:
Expand Down
7 changes: 1 addition & 6 deletions pyzbar/wrapper.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions pyzbar/zbar_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit be33da1

Please sign in to comment.