Back to index

python-biopython  1.60
test_File.py
Go to the documentation of this file.
00001 # Copyright 1999 by Jeffrey Chang.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 from __future__ import with_statement
00007 
00008 import os.path
00009 import unittest
00010 import shutil
00011 from StringIO import StringIO
00012 import tempfile
00013 
00014 
00015 from Bio import File
00016 
00017 
00018 
00019 data = """This
00020 is
00021 a multi-line
00022 file"""
00023 
00024 
00025 
00026 ### StringHandle
00027 
00028 h = File.StringHandle(data)
00029 print repr(h.readline())  # 'This'
00030 print len(h.readlines())  # 3
00031 print repr(h.readline())  # ''
00032 h.close()
00033 
00034 
00035 
00036 ### UndoHandle
00037 
00038 h = File.UndoHandle(File.StringHandle(data))
00039 
00040 print h.readline()   # 'This'
00041 print h.peekline()   # 'is'
00042 print h.readline()   # 'is'
00043 h.saveline("saved")
00044 print h.peekline()   # 'saved'
00045 h.saveline("another")
00046 print h.readline()   # 'another'
00047 print h.readline()   # 'saved'
00048 
00049 # Test readlines after saveline
00050 h.saveline("saved again")
00051 lines = h.readlines()
00052 print repr(lines[0])   # 'saved again'
00053 print repr(lines[1])   # 'a multi-line'
00054 print repr(lines[2])   # 'file'
00055 
00056 # should be empty now
00057 print repr(h.readline())       # ''
00058 
00059 h.saveline("save after empty")
00060 print h.readline()             # 'save after empty'
00061 print repr(h.readline())       # ''
00062 
00063 # test read method
00064 h = File.UndoHandle(File.StringHandle("some text"))
00065 h.saveline("more text")
00066 print h.read()                 # 'more textsome text'
00067 
00068 class AsHandleTestCase(unittest.TestCase):
00069 
00070     def setUp(self):
00071         # Create a directory to work in
00072         self.temp_dir = tempfile.mkdtemp(prefix='biopython-test')
00073 
00074     def tearDown(self):
00075         shutil.rmtree(self.temp_dir)
00076 
00077     def _path(self, *args):
00078         return os.path.join(self.temp_dir, *args)
00079 
00080     def test_handle(self):
00081         "Test as_handle with a file-like object argument"
00082         p = self._path('test_file.fasta')
00083         with open(p, 'wb') as fp:
00084             with File.as_handle(fp) as handle:
00085                 self.assertEqual(fp, handle, "as_handle should "
00086                         "return argument when given a file-like object")
00087                 self.assertFalse(handle.closed)
00088 
00089             self.assertFalse(handle.closed,
00090                     "Exiting as_handle given a file-like object should not "
00091                     "close the file")
00092 
00093     def test_path(self):
00094         "Test as_handle with a path argument"
00095         p = self._path('test_file.fasta')
00096         mode = 'wb'
00097         with File.as_handle(p, mode=mode) as handle:
00098             self.assertEqual(p, handle.name)
00099             self.assertEqual(mode, handle.mode)
00100             self.assertFalse(handle.closed)
00101         self.assertTrue(handle.closed)
00102 
00103     def test_stringio(self):
00104         s = StringIO()
00105         with File.as_handle(s) as handle:
00106             self.assertEqual(s, handle)