def test_startswith_exhaustive(ending): # Exhaustive test of the various combinations. Should catch most # edge conditions. for base in ("A" + ending, "A" + ending + "BA" + ending): for repeat in range(0, 15): s = base * repeat infile = StringIO(s) #for marker in ("A", "A\n"): # Don't use; bug when using "\n" for marker in ("A", ): for look in range(5): lookahead = base * look for readhint in range(4, 10): infile.seek(0) reader = RecordReader.StartsWith(\ infile, marker, readhint, lookahead) count = count_records(reader) assert count == repeat + look, \ (count, ending, base, repeat, marker, look, readhint) infile.seek(0) reader = RecordReader.StartsWith(infile, marker, lookahead=lookahead) count = count_records(reader) assert count == repeat + look, \ (count, ending, repeat, marker, look)
def test_startswith_SP(): # Check using a SWISS-PROT-like format for ending in ("\n", "\r", "\r\n"): s = string.replace(data1, "\n", ending) for final in ("", ending): d = s + final for i in range(5, 20): infile = StringIO(d) reader = RecordReader.StartsWith(infile, "ID", i) count = count_records(reader) assert count == 3, (ending, final, i, count, d) for i in range(6, 20): infile = StringIO(d) reader = RecordReader.StartsWith(infile, "ID ", i) count = count_records(reader) assert count == 3, (ending, final, i, count, d)
def __init__(self, handle, parser=None): """Initialize the iterator. Arguments: o handle - A handle with NBRF entries to iterate through. o parser - An optional parser to pass the entries through before returning them. If None, then the raw entry will be returned. """ self.handle = File.UndoHandle(handle) self._reader = RecordReader.StartsWith(self.handle, ">") self._parser = parser
def __init__(self, handle, parser = None): """Initialize the iterator. Arguments: o handle - A handle with Kabat entries to iterate through. o parser - An optional parser to pass the entries through before returning them. If None, then the raw entry will be returned. """ self._reader = RecordReader.StartsWith(handle, "METATOOL") # self._reader = RecordReader.EndsWith(handle, "RECEND|\n") self._parser = parser
def test_fasta(): s = """\ >seq1 SEQUENCE >seq2 MULTI LINE SEQUENCE >seq3 >seq4 """ reader = RecordReader.StartsWith(StringIO(s), ">") assert test_count(reader) == 4 reader = RecordReader.StartsWith(StringIO(""), ">") assert test_count(reader) == 0 reader = RecordReader.StartsWith(StringIO(">seq\n"), ">") assert test_count(reader) == 1
def test_startswith_generic(): to_eol_data = ( ("A\n", 1), ("AA\n", 1), ("A\nB\n", 1), ("A\nB\nB\n", 1), ("A\nB\nB\nA\n", 2), ("A\nA\nB\nA\n", 3), ("A\n A\nA\n", 2), ("A A A A A A A A A A A A A A\nB\nA\n", 2), ) for s, expected in to_eol_data: reader = RecordReader.StartsWith(StringIO(s), "A") count = count_records(reader) assert count == expected, (s, expected, count) # skips to EOL
def test_startswith_errors(): # Check the failure cases. Actually, there's only one. # Doesn't start with A for s in ("B", "B\n", " A\n", " A", "B\nA\n"): try: infile = StringIO(s) # The current implementation will fail here, but the # interface spec allows the error to be unreported # until reading the record. reader = RecordReader.StartsWith(infile, "A") rec = reader.next() raise AssertionError, "should not allow %r" % s except RecordReader.ReaderError: pass else: raise AssertionError, "should not get here"
def test_startswith_remainder(): # Make sure the remainder method works for repeat in range(20): vals = map(lambda x: "A\n%d\n" % x, range(repeat)) data = string.join(vals, "") infile = StringIO(data) for look in range(10) + range(10, len(data), 5): infile.seek(look) lookahead = data[:look] reader = RecordReader.StartsWith(infile, "A", lookahead=lookahead) all = "" while 1: file, lh = reader.remainder() pos = file.tell() rest = file.read() assert all + lh + rest == data, (all, lh, rest, data) file.seek(pos) assert data.startswith(all), (data, all) record = reader.next() if record is None: break all = all + record assert all == data, (all, data)
def test_start_lines(): lookahead = open(sp_sample).read() reader = RecordReader.StartsWith(StringIO(""), "ID", lookahead = lookahead) assert test_count(reader) == 8
def test_start(): reader = RecordReader.StartsWith(open(sp_sample), "ID") assert test_count(reader, 0) == 8