Permalink
Browse files

Merge pull request #69 from cgohlke/patch-1

Close open files in IO instances
  • Loading branch information...
2 parents 5b621ae + 98b992b commit 59a38a386c635d5f9f346cf257ab7b7ce1e31a47 @kwgoodman committed Oct 16, 2013
Showing with 67 additions and 58 deletions.
  1. +9 −0 la/io.py
  2. +58 −58 la/tests/io_test.py
View
@@ -180,6 +180,15 @@ def merge(self, key, lar, update=False):
lar2 = lar1.merge(lar, update=update)
del self.f[key]
self[key] = lar2
+
+ def close(self):
+ self.f.close()
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.close()
def __iter__(self):
return iter(self.keys())
View
@@ -28,82 +28,82 @@ def tearDown(self):
def test_io_1(self):
"io_general"
- io = IO(self.filename)
- x = larry([1,2,3])
- io['x'] = x
- self.assertTrue('x' in io, 'key missing')
- self.assertTrue((x == io['x'][:]).all(), 'save and load difference')
- self.assertTrue(['x'] == io.keys(), 'keys are different')
- self.assertTrue(x.dtype == io['x'].dtype, 'dtype changed')
- del io['x']
- self.assertTrue(io.keys() == [], 'key still present')
+ with IO(self.filename) as io:
+ x = larry([1,2,3])
+ io['x'] = x
+ self.assertTrue('x' in io, 'key missing')
+ self.assertTrue((x == io['x'][:]).all(), 'save and load difference')
+ self.assertTrue(['x'] == list(io.keys()), 'keys are different')
+ self.assertTrue(x.dtype == io['x'].dtype, 'dtype changed')
+ del io['x']
+ self.assertTrue(list(io.keys()) == [], 'key still present')
def test_io_2(self):
"io_repack"
- io = IO(self.filename)
- io['larry'] = la.rand(100, 100)
- fs1 = io.freespace
- sp1 = io.space
- del io['larry']
- io.repack()
- fs2 = io.freespace
- sp2 = io.space
- self.assertTrue(fs2 < fs1, 'repack did not reduce freespace')
- self.assertTrue(sp2 < sp1, 'repack did not reduce space')
+ with IO(self.filename) as io:
+ io['larry'] = la.rand(100, 100)
+ fs1 = io.freespace
+ sp1 = io.space
+ del io['larry']
+ io.repack()
+ fs2 = io.freespace
+ sp2 = io.space
+ self.assertTrue(fs2 < fs1, 'repack did not reduce freespace')
+ self.assertTrue(sp2 < sp1, 'repack did not reduce space')
def test_io_3(self):
"io_keys"
- io = IO(self.filename)
- io['1'] = larry([1,2,3])
- io['2'] = larry([1,2,3])
- io.f['3'] = [1,2,3]
- io['1/2/3/4'] = larry([1,2,3])
- keys = io.keys()
- keys.sort()
- theory = ['1', '1/2/3/4', '2']
- self.assertTrue(keys == theory, 'keys do not match')
+ with IO(self.filename) as io:
+ io['1'] = larry([1,2,3])
+ io['2'] = larry([1,2,3])
+ io.f['3'] = [1,2,3]
+ io['1/2/3/4'] = larry([1,2,3])
+ keys = list(io.keys())
+ keys.sort()
+ theory = ['1', '1/2/3/4', '2']
+ self.assertTrue(keys == theory, 'keys do not match')
def test_io_4(self):
"io_dates"
- io = IO(self.filename)
- x = [1, 2]
- label = [[datetime.date(2010,3,1), datetime.date(2010,3,2)]]
- desired = larry(x, label)
- io['desired'] = desired
- actual = io['desired'][:]
- assert_larry_equal(actual, desired)
+ with IO(self.filename) as io:
+ x = [1, 2]
+ label = [[datetime.date(2010,3,1), datetime.date(2010,3,2)]]
+ desired = larry(x, label)
+ io['desired'] = desired
+ actual = io['desired'][:]
+ assert_larry_equal(actual, desired)
def test_io_5(self):
"io_datetimes"
- io = IO(self.filename)
- x = [1, 2]
- label = [[datetime.datetime(2010,3,1,13,15,59,9998),
- datetime.datetime(2010,3,2,11,23)]]
- desired = larry(x, label)
- io['desired'] = desired
- actual = io['desired'][:]
- assert_larry_equal(actual, desired)
+ with IO(self.filename) as io:
+ x = [1, 2]
+ label = [[datetime.datetime(2010,3,1,13,15,59,9998),
+ datetime.datetime(2010,3,2,11,23)]]
+ desired = larry(x, label)
+ io['desired'] = desired
+ actual = io['desired'][:]
+ assert_larry_equal(actual, desired)
def test_io_6(self):
"io_datetimes"
- io = IO(self.filename)
- x = [1, 2]
- label = [[datetime.time(13,15,59,9998),
- datetime.time(11,23)]]
- desired = larry(x, label)
- io['desired'] = desired
- actual = io['desired'][:]
- assert_larry_equal(actual, desired)
+ with IO(self.filename) as io:
+ x = [1, 2]
+ label = [[datetime.time(13,15,59,9998),
+ datetime.time(11,23)]]
+ desired = larry(x, label)
+ io['desired'] = desired
+ actual = io['desired'][:]
+ assert_larry_equal(actual, desired)
def test_io_7(self):
"io_empty (gh #68)"
- io = IO(self.filename)
- desired = larry([])
- io['desired'] = desired
- actual = io['desired']
- if actual.size == 0:
- actual = la.larry([])
- assert_larry_equal(actual, desired)
+ with IO(self.filename) as io:
+ desired = larry([])
+ io['desired'] = desired
+ actual = io['desired']
+ if actual.size == 0:
+ actual = la.larry([])
+ assert_larry_equal(actual, desired)
# nose tests ----------------------------------------------------------------

0 comments on commit 59a38a3

Please sign in to comment.