Skip to content

Commit

Permalink
Use shared Python 2/3 compatibility module (#50)
Browse files Browse the repository at this point in the history
* Incorporate more Python 2/3 compatibility

There is some other Python 2/3 compatibility code being used in
`dask-image`, which needed to be consolidated in the share Python
compatibility module. This adds that shared code.

* Test additional Python 2/3 compatibility code

* Use Python compatibility code in `ndfilters`

Update `ndfilters` utility code to make use of the share Python 2/3
compatibility code.

* Use Python compatibility code in `ndfourier`

Update `ndfourier` utility code to make use of the share Python 2/3
compatibility code.

* Use Python compatibility code in `ndmorph`

Update `ndmorph` utility code to make use of the share Python 2/3
compatibility code.
  • Loading branch information
jakirkham committed Sep 2, 2018
1 parent 693db91 commit edf472f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 52 deletions.
11 changes: 9 additions & 2 deletions dask_image/_pycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
irange = range

try:
from itertools import izip
from itertools import imap, izip
except ImportError:
izip = zip
imap, izip = map, zip

try:
unicode = unicode
except NameError:
unicode = str

strlike = (bytes, unicode)
18 changes: 1 addition & 17 deletions dask_image/ndfilters/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,7 @@

import numpy


try:
from itertools import imap, izip
except ImportError:
imap, izip = map, zip

try:
irange = xrange
except NameError:
irange = range

try:
unicode
except NameError:
unicode = str

strlike = (bytes, unicode)
from .._pycompat import imap, irange, izip, strlike, unicode


def _get_docstring(func):
Expand Down
11 changes: 1 addition & 10 deletions dask_image/ndfourier/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
import dask.array

from . import _compat

try:
from itertools import imap
except ImportError:
imap = map

try:
irange = xrange
except NameError:
irange = range
from .._pycompat import imap, irange


def _get_freq_grid(shape, chunks, dtype=float):
Expand Down
7 changes: 1 addition & 6 deletions dask_image/ndmorph/_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
import dask.array

from . import _utils


try:
irange = xrange
except NameError:
irange = range
from .._pycompat import irange


def _where(condition, x, y):
Expand Down
18 changes: 1 addition & 17 deletions dask_image/ndmorph/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,7 @@

import dask.array


try:
from itertools import imap, izip
except ImportError:
imap, izip = map, zip

try:
irange = xrange
except NameError:
irange = range

try:
unicode
except NameError:
unicode = str

strlike = (bytes, unicode)
from .._pycompat import imap, irange, izip, strlike, unicode


def _get_docstring(func):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dask_image/test__pycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,31 @@ def test_irange():
assert list(r) == [0, 1, 2, 3, 4]


def test_imap():
r = dask_image._pycompat.imap(lambda e : e ** 2, [0, 1, 2, 3])

assert not isinstance(r, list)

assert list(r) == [0, 1, 4, 9]


def test_izip():
r = dask_image._pycompat.izip([1, 2], [3, 4, 5])

assert not isinstance(r, list)

assert list(r) == [(1, 3), (2, 4)]


def test_strlike():
b = b"Hello World!"
u = u"Hello World!"

assert isinstance(b, dask_image._pycompat.strlike)
assert isinstance(u, dask_image._pycompat.strlike)


def test_unicode():
u = u"Hello World!"

assert isinstance(u, dask_image._pycompat.unicode)

0 comments on commit edf472f

Please sign in to comment.