def test_close(self): """close() flushes the buffer""" b = BufferedWriter(self.test_fp, buf_size=2) b.write("1") b.close() content = open(self.test_fp, "r").readlines() self.assertEquals(content, ["1"])
def _split_along_prefix(self, input_fp, params, jobs_to_start, job_prefix, output_dir): """ Split input sequences into sets with identical prefix""" out_files = [] buffered_handles = {} prefix_length = params['prefix_length'] or 1 for seq_id, seq in parse_fasta(open(input_fp)): if (len(seq) < prefix_length): raise ValueError( "Prefix length must be equal or longer than sequence.\n" + " Found seq %s with length %d" % (seq_id, len(seq))) prefix = seq[:prefix_length] if (prefix not in buffered_handles): # never seen this prefix before out_fp = "%s/%s%s" % (output_dir, job_prefix, prefix) buffered_handles[prefix] = BufferedWriter(out_fp) out_files.append(out_fp) self.prefix_counts[prefix] = 0 self.prefix_counts[prefix] += 1 buffered_handles[prefix].write('>%s\n%s\n' % (seq_id, seq)) # make sure all buffers are closed and flushed for buf_fh in buffered_handles.itervalues(): buf_fh.close() remove_files = True return out_files, remove_files
def test_write(self): """BufferedWriter writes nothing until max buffer reached.""" b = BufferedWriter(self.test_fp, buf_size=2) b.write("1") content = open(self.test_fp, "r").readlines() self.assertEquals(content, []) # still nothing b.write("2") content = open(self.test_fp, "r").readlines() self.assertEquals(content, []) # finally, buffer is flushed b.write("3") content = open(self.test_fp, "r").readlines() self.assertEquals(content, ["123"])
def test_init(self): """BufferedWriter constructor works""" b = BufferedWriter(self.test_fp) self.assertTrue(exists(self.test_fp))