Skip to content

Commit fdf700f

Browse files
author
sprenger
committed
[EDF] Add context manager capabilities, __del__ and test
1 parent d16fd4c commit fdf700f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

neo/rawio/edfrawio.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,21 @@ def _rescale_event_timestamp(self, event_timestamps, dtype, event_channel_index)
223223
def _rescale_epoch_duration(self, raw_duration, dtype, event_channel_index):
224224
return None
225225

226+
def __enter__(self):
227+
return self
228+
229+
def __del__(self):
230+
self._close_reader()
231+
232+
def __exit__(self, exc_type, exc_val, ex_tb):
233+
self._close_reader()
234+
226235
def close(self):
227-
self.edf_reader.close()
236+
"""
237+
Closes the file handler
238+
"""
239+
self._close_reader()
240+
241+
def _close_reader(self):
242+
if hasattr(self, 'edf_reader'):
243+
self.edf_reader.close()

neo/test/rawiotest/test_edfrawio.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ class TestExampleRawIO(BaseTestRawIO, unittest.TestCase, ):
1515
'edf/edf+C.edf',
1616
]
1717

18+
def test_context_handler(self):
19+
filename = self.get_local_path('edf/edf+C.edf')
20+
with EDFRawIO(filename) as io:
21+
io.parse_header()
22+
23+
# Check that file was closed properly and can be opened again
24+
with open(filename) as f:
25+
pass
26+
27+
28+
def test_close(self):
29+
filename = self.get_local_path('edf/edf+C.edf')
30+
31+
# Open file and close it again
32+
io1 = EDFRawIO(filename)
33+
io1.parse_header()
34+
io1.close()
35+
36+
# Check that file was closed properly and can be opened again
37+
io2 = EDFRawIO(filename)
38+
io2.parse_header()
39+
io2.close()
40+
1841

1942
if __name__ == "__main__":
2043
unittest.main()

0 commit comments

Comments
 (0)