Skip to content

Commit 382dcf8

Browse files
committed
RF: Make _gzip_open a bit cleaner and simpler. Move the DummyLock out of
read_segments, and to the model level. Cleaned up ArrayProxy._get_fileobj docs (and replaced all triple-single quotes with triple-double quotes)
1 parent 388daae commit 382dcf8

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

nibabel/arrayproxy.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,21 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open=False):
135135
self._lock = Lock()
136136

137137
def __del__(self):
138-
'''If this ``ArrayProxy`` was created with ``keep_file_open=True``,
138+
"""If this ``ArrayProxy`` was created with ``keep_file_open=True``,
139139
the open file object is closed if necessary.
140-
'''
140+
"""
141141
if hasattr(self, '_opener') and not self._opener.closed:
142142
self._opener.close()
143143
self._opener = None
144144

145145
def __getstate__(self):
146-
'''Returns the state of this ``ArrayProxy`` during pickling. '''
146+
"""Returns the state of this ``ArrayProxy`` during pickling. """
147147
state = self.__dict__.copy()
148148
state.pop('_lock', None)
149149
return state
150150

151151
def __setstate__(self, state):
152-
'''Sets the state of this ``ArrayProxy`` during unpickling. '''
152+
"""Sets the state of this ``ArrayProxy`` during unpickling. """
153153
self.__dict__.update(state)
154154
self._lock = Lock()
155155

@@ -184,10 +184,18 @@ def is_proxy(self):
184184

185185
@contextmanager
186186
def _get_fileobj(self):
187-
'''Create and return a new ``ImageOpener``, or return an existing one.
188-
one. The specific behaviour depends on the value of the
189-
``keep_file_open`` flag that was passed to ``__init__``.
190-
'''
187+
"""Create and return a new ``ImageOpener``, or return an existing one.
188+
189+
The specific behaviour depends on the value of the ``keep_file_open``
190+
flag that was passed to ``__init__``.
191+
192+
193+
Yields
194+
------
195+
ImageOpener
196+
A newly created ``ImageOpener`` instance, or an existing one,
197+
which provides access to the file.
198+
"""
191199
if self._keep_file_open:
192200
if not hasattr(self, '_opener'):
193201
self._opener = ImageOpener(self.file_like)
@@ -197,10 +205,10 @@ def _get_fileobj(self):
197205
yield opener
198206

199207
def get_unscaled(self):
200-
''' Read of data from file
208+
""" Read of data from file
201209
202210
This is an optional part of the proxy API
203-
'''
211+
"""
204212
with self._get_fileobj() as fileobj:
205213
raw_data = array_from_file(self._shape,
206214
self._dtype,
@@ -228,7 +236,7 @@ def __getitem__(self, slicer):
228236
return apply_read_scaling(raw_data, self._slope, self._inter)
229237

230238
def reshape(self, shape):
231-
''' Return an ArrayProxy with a new shape, without modifying data '''
239+
""" Return an ArrayProxy with a new shape, without modifying data """
232240
size = np.prod(self._shape)
233241

234242
# Calculate new shape if not fully specified

nibabel/fileslice.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717
SKIP_THRESH = 2 ** 8
1818

1919

20+
21+
class _NullLock(object):
22+
"""The ``_NullLock`` is an object which can be used in place of a
23+
``threading.Lock`` object, but doesn't actually do anything.
24+
25+
It is used by the ``read_segments`` function in the event that a
26+
``Lock`` is not provided by the caller.
27+
"""
28+
def __enter__(self):
29+
pass
30+
31+
def __exit__(self, exc_type, exc_val, exc_tb):
32+
return False
33+
34+
2035
def is_fancy(sliceobj):
2136
""" Returns True if sliceobj is attempting fancy indexing
2237
@@ -647,17 +662,9 @@ def read_segments(fileobj, segments, n_bytes, lock=None):
647662
object implementing buffer protocol, such as byte string or ndarray or
648663
mmap or ctypes ``c_char_array``
649664
"""
650-
# Make a dummy lock-like thing to make the code below a bit nicer
665+
# Make a lock-like thing to make the code below a bit nicer
651666
if lock is None:
652-
653-
class DummyLock(object):
654-
def __enter__(self):
655-
pass
656-
657-
def __exit__(self, exc_type, exc_val, exc_tb):
658-
return False
659-
660-
lock = DummyLock()
667+
lock = _NullLock()
661668

662669
if len(segments) == 0:
663670
if n_bytes != 0:

nibabel/openers.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,18 @@ def _gzip_open(fileish, mode='rb', compresslevel=9):
7070
have_indexed_gzip = False
7171

7272
# is this a file? if not we assume it is a string
73-
is_file = (hasattr(fileish, 'read') and hasattr(fileish, 'write') and
74-
hasattr(fileish, 'mode'))
73+
is_file = hasattr(fileish, 'read') and hasattr(fileish, 'write')
7574

7675
# If we've been given a file object, we can't change its mode.
77-
if is_file:
76+
if is_file and hasattr(fileish, 'mode'):
7877
mode = fileish.mode
7978

8079
# use indexed_gzip if possible for faster read access
8180
if mode == 'rb' and have_indexed_gzip:
82-
kwargs = {'spacing':4194304, 'readbuf_size':1048576}
83-
if hasattr(fileish, 'read') and hasattr(fileish, 'write'):
84-
gzip_file = SafeIndexedGzipFile(fid=fileish, **kwargs)
81+
if is_file:
82+
gzip_file = SafeIndexedGzipFile(fid=fileish)
8583
else:
86-
gzip_file = SafeIndexedGzipFile(filename=fileish, **kwargs)
84+
gzip_file = SafeIndexedGzipFile(filename=fileish)
8785

8886
# Fall-back to built-in GzipFile (wrapped with the BufferedGzipFile class
8987
# defined above)

0 commit comments

Comments
 (0)