Skip to content

Commit

Permalink
Addressing biocore#77
Browse files Browse the repository at this point in the history
  • Loading branch information
mortonjt committed Dec 2, 2016
1 parent 7f6a06b commit b6be1ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# gneiss changelog

## Version 0.2.0

### Features
* Added filehandle support for write and read io in RegressionResults object [#72](https://github.com/biocore/gneiss/issues/77)


## Version 0.1.3

### Features
Expand Down
19 changes: 12 additions & 7 deletions gneiss/regression/_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def read_pickle(self, filename):
Parameters
----------
filename : str
filename : str or filehandle
Input file to unpickle.
Returns
Expand All @@ -283,18 +283,23 @@ def read_pickle(self, filename):
Warning: Loading pickled data received from untrusted
sources can be unsafe. See: https://wiki.python.org/moin/UsingPickle
"""
with open(filename, 'rb') as fh:
res = pickle.load(fh)
if isinstance(filename, str):
with open(filename, 'rb') as fh:
res = pickle.load(fh)
else:
res = pickle.load(filename)
return res

def write_pickle(self, filename):
""" Writes RegressionResults object to pickle file.
Parameters
----------
filename : str
filename : str or filehandle
Output file to store pickled object.
"""

with open(filename, 'wb') as fh:
pickle.dump(self, fh)
if isinstance(filename, str):
with open(filename, 'wb') as fh:
pickle.dump(self, fh)
else:
pickle.dump(self, filename)
11 changes: 11 additions & 0 deletions gneiss/regression/tests/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ def test_read_write(self):
self.assertEqual(str(res.tree), str(exp.tree))
pdt.assert_frame_equal(res.pvalues, exp.pvalues)

def test_read_write_handle(self):
fh = open(self.pickle_fname, 'wb')
exp = RegressionResults(self.results, tree=self.tree)
exp.write_pickle(fh)
fh.close()
fh = open(self.pickle_fname, 'rb')
res = RegressionResults.read_pickle(self.pickle_fname)
fh.close()
self.assertEqual(str(res.tree), str(exp.tree))
pdt.assert_frame_equal(res.pvalues, exp.pvalues)


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

0 comments on commit b6be1ba

Please sign in to comment.