From 0f8ae15d2207d5eaf96066dc4e9e5576973cd5a2 Mon Sep 17 00:00:00 2001 From: Vincent Davis Date: Thu, 3 Dec 2015 19:19:35 -0700 Subject: [PATCH] use with open() --- Tests/test_SeqIO_FastaIO.py | 32 +++++++++++++++----------------- Tests/test_prosite2.py | 21 +++++++++------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/Tests/test_SeqIO_FastaIO.py b/Tests/test_SeqIO_FastaIO.py index 20389b96178..e7f1868b629 100644 --- a/Tests/test_SeqIO_FastaIO.py +++ b/Tests/test_SeqIO_FastaIO.py @@ -44,29 +44,27 @@ def title_to_ids(title): def read_single_with_titles(filename, alphabet): global title_to_ids - handle = open(filename) - iterator = FastaIterator(handle, alphabet, title_to_ids) - record = next(iterator) - try: - second = next(iterator) - except StopIteration: - second = None - handle.close() + with open(filename) as handle: + iterator = FastaIterator(handle, alphabet, title_to_ids) + record = next(iterator) + try: + second = next(iterator) + except StopIteration: + second = None assert record is not None and second is None return record def read_title_and_seq(filename): """Crude parser that gets the first record from a FASTA file.""" - handle = open(filename) - title = handle.readline().rstrip() - assert title.startswith(">") - seq = "" - for line in handle: - if line.startswith(">"): - break - seq += line.strip() - handle.close() + with open(filename) as handle: + title = handle.readline().rstrip() + assert title.startswith(">") + seq = "" + for line in handle: + if line.startswith(">"): + break + seq += line.strip() return title[1:], seq diff --git a/Tests/test_prosite2.py b/Tests/test_prosite2.py index e4b2cd7919d..53be98d2c47 100644 --- a/Tests/test_prosite2.py +++ b/Tests/test_prosite2.py @@ -14,11 +14,10 @@ class TestPrositeRead(unittest.TestCase): def test_read4(self): - "Parsing Prosite record ps00432.txt" + """Parsing Prosite record ps00432.txt""" filename = os.path.join('Prosite', 'ps00432.txt') - handle = open(filename) - record = Prosite.read(handle) - handle.close() + with open(filename) as handle: + record = Prosite.read(handle) self.assertEqual(record.name, "ACTINS_2") self.assertEqual(record.type, "PATTERN") self.assertEqual(record.accession, "PS00432") @@ -453,11 +452,10 @@ def read4_false_etc(self, record): self.assertEqual(record.pdb_structs[59], '2BTF') def test_read5(self): - "Parsing Prosite record ps00488.txt" + """Parsing Prosite record ps00488.txt""" filename = os.path.join('Prosite', 'ps00488.txt') - handle = open(filename) - record = Prosite.read(handle) - handle.close() + with open(filename) as handle: + record = Prosite.read(handle) self.assertEqual(record.name, "PAL_HISTIDASE") self.assertEqual(record.type, "PATTERN") self.assertEqual(record.accession, "PS00488") @@ -621,11 +619,10 @@ def test_read5(self): self.assertEqual(record.pdb_structs[5], "1Y2M") def test_read6(self): - "Parsing Prosite record ps00546.txt" + """Parsing Prosite record ps00546.txt""" filename = os.path.join('Prosite', 'ps00546.txt') - handle = open(filename) - record = Prosite.read(handle) - handle.close() + with open(filename) as handle: + record = Prosite.read(handle) self.assertEqual(record.name, "CYSTEINE_SWITCH") self.assertEqual(record.type, "PATTERN") self.assertEqual(record.accession, "PS00546")