Skip to content

Commit

Permalink
use with open()
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdavis authored and peterjc committed May 23, 2016
1 parent b189c45 commit 0f8ae15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
32 changes: 15 additions & 17 deletions Tests/test_SeqIO_FastaIO.py
Expand Up @@ -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


Expand Down
21 changes: 9 additions & 12 deletions Tests/test_prosite2.py
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 0f8ae15

Please sign in to comment.