Skip to content

Commit 9b43713

Browse files
committed
RF: Import from numpy.compat.py3k when needed
1 parent a407bf3 commit 9b43713

File tree

11 files changed

+18
-98
lines changed

11 files changed

+18
-98
lines changed

nibabel/cmdline/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import numpy as np
2020

21-
from nibabel.py3k import asunicode
22-
2321
verbose_level = 0
2422

2523

@@ -100,7 +98,7 @@ def table2string(table, out=None):
10098
string_ += "%%%ds%%s%%%ds " \
10199
% (nspacesl, nspacesr) % ('', item, '')
102100
string += string_.rstrip() + '\n'
103-
out.write(asunicode(string))
101+
out.write(string)
104102

105103
if print2string:
106104
value = out.getvalue()

nibabel/externals/netcdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from mmap import mmap, ACCESS_READ
3636

3737
import numpy as np # noqa
38-
from ..py3k import asbytes, asstr
38+
from numpy.compat.py3k import asbytes, asstr
3939
from numpy import frombuffer, ndarray, dtype, empty, array, asarray
4040
from numpy import little_endian as LITTLE_ENDIAN
4141
from functools import reduce

nibabel/nicom/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" Utilities for working with DICOM datasets
22
"""
33

4-
from ..py3k import asstr
4+
from numpy.compat.py3k import asstr
55

66

77
def find_private_section(dcm_data, group_no, creator):

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import numpy as np
1717
import numpy.linalg as npl
18+
from numpy.compat.py3k import asstr
1819

19-
from .py3k import asstr
2020
from .filebasedimages import SerializableImage
2121
from .volumeutils import Recoder, make_dt_codes, endian_codes
2222
from .spatialimages import HeaderDataError, ImageFileError

nibabel/py3k.py

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,9 @@
1-
"""
2-
Python 3 compatibility tools.
1+
import warnings
32

4-
Copied from numpy/compat/py3k.
3+
warnings.warn("We no longer carry a copy of the 'py3k' module in nibabel; "
4+
"Please import from the 'numpy.compat.py3k' module directly. "
5+
"Full removal scheduled for nibabel 4.0.",
6+
FutureWarning,
7+
stacklevel=2)
58

6-
Please prefer the routines in the six module when possible.
7-
8-
BSD license
9-
"""
10-
11-
__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
12-
'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
13-
'asstr', 'open_latin1', 'StringIO', 'BytesIO']
14-
15-
import sys
16-
17-
if sys.version_info[0] >= 3:
18-
import io
19-
StringIO = io.StringIO
20-
BytesIO = io.BytesIO
21-
bytes = bytes
22-
unicode = str
23-
asunicode = str
24-
25-
def asbytes(s):
26-
if isinstance(s, bytes):
27-
return s
28-
return s.encode('latin1')
29-
30-
def asstr(s):
31-
if isinstance(s, str):
32-
return s
33-
return s.decode('latin1')
34-
35-
def isfileobj(f):
36-
return isinstance(f, io.FileIO)
37-
38-
def open_latin1(filename, mode='r'):
39-
return open(filename, mode=mode, encoding='iso-8859-1')
40-
strchar = 'U'
41-
ints2bytes = lambda seq: bytes(seq)
42-
ZEROB = bytes([0])
43-
FileNotFoundError = FileNotFoundError
44-
import builtins
45-
else:
46-
import StringIO
47-
StringIO = BytesIO = StringIO.StringIO
48-
bytes = str
49-
unicode = unicode
50-
asbytes = str
51-
asstr = str
52-
strchar = 'S'
53-
54-
def isfileobj(f):
55-
return isinstance(f, file)
56-
57-
def asunicode(s):
58-
if isinstance(s, unicode):
59-
return s
60-
return s.decode('ascii')
61-
62-
def open_latin1(filename, mode='r'):
63-
return open(filename, mode=mode)
64-
ints2bytes = lambda seq: ''.join(chr(i) for i in seq)
65-
ZEROB = chr(0)
66-
67-
class FileNotFoundError(IOError):
68-
pass
69-
70-
import __builtin__ as builtins # noqa
71-
72-
73-
def getexception():
74-
return sys.exc_info()[1]
75-
76-
77-
def asbytes_nested(x):
78-
if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
79-
return [asbytes_nested(y) for y in x]
80-
else:
81-
return asbytes(x)
82-
83-
84-
def asunicode_nested(x):
85-
if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):
86-
return [asunicode_nested(y) for y in x]
87-
else:
88-
return asunicode(x)
9+
from numpy.compat.py3k import * # noqa

nibabel/streamlines/tck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import warnings
99

1010
import numpy as np
11+
from numpy.compat.py3k import asbytes, asstr
1112

1213
from nibabel.openers import Opener
13-
from nibabel.py3k import asbytes, asstr
1414

1515
from .array_sequence import ArraySequence
1616
from .tractogram_file import TractogramFile

nibabel/streamlines/tests/test_streamlines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import nibabel as nib
99
from io import BytesIO
1010
from nibabel.tmpdirs import InTemporaryDirectory
11-
from nibabel.py3k import asbytes
11+
from numpy.compat.py3k import asbytes
1212

1313
from nibabel.testing import data_path
1414
from nibabel.testing import clear_and_catch_warnings

nibabel/streamlines/trk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import warnings
99

1010
import numpy as np
11+
from numpy.compat.py3k import asstr
12+
1113
import nibabel as nib
1214

1315
from nibabel.openers import Opener
14-
from nibabel.py3k import asstr
1516
from nibabel.volumeutils import (native_code, swapped_code, endian_codes)
1617
from nibabel.orientations import (aff2axcodes, axcodes2ornt)
1718

nibabel/tests/test_openers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from io import BytesIO, UnsupportedOperation
1414
from distutils.version import StrictVersion
1515

16-
from ..py3k import asstr, asbytes
16+
from numpy.compat.py3k import asstr, asbytes
1717
from ..openers import Opener, ImageOpener, HAVE_INDEXED_GZIP, BZ2File
1818
from ..tmpdirs import InTemporaryDirectory
1919
from ..volumeutils import BinOpener

nibabel/tests/test_optpkg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import mock
55
import types
66
import sys
7+
import builtins
78
from distutils.version import LooseVersion
89

910
from nose import SkipTest
1011
from nose.tools import (assert_true, assert_false, assert_raises,
1112
assert_equal)
1213

1314

14-
from nibabel.py3k import builtins
1515
from nibabel.optpkg import optional_package
1616
from nibabel.tripwire import TripWire, TripWireError
1717

0 commit comments

Comments
 (0)