def test_read_write_file_lines_gzip(self): expected = [sent.text for sent in self.spacy_doc.sents] filename = os.path.join(self.tempdir, 'test_read_write_file_lines.txt.gzip') fileio.write_file_lines(expected, filename) observed = [line.strip() for line in fileio.read_file_lines(filename)] self.assertEqual(observed, expected)
def test_read_write_file_lines_bz2(self): expected = [sent.text for sent in self.spacy_doc.sents] filename = os.path.join(self.tempdir, 'test_read_write_file_lines.txt.bz2') fileio.write_file_lines(expected, filename, mode='wb' if PY2 else 'wt') observed = [line.strip() for line in fileio.read_file_lines(filename, mode='r' if PY2 else 'rt')] self.assertEqual(observed, expected)
def test_read_write_file_lines_bz2(self): expected = [sent.text for sent in self.spacy_doc.sents] tempdir = tempfile.mkdtemp() filename = os.path.join(tempdir, 'test_read_write_file_lines.txt.bz2') fileio.write_file_lines(expected, filename) observed = [line.strip() for line in fileio.read_file_lines(filename)] os.remove(filename) os.rmdir(tempdir) self.assertEqual(observed, expected)
def test_read_write_file_lines_bytes(self): expected = [unicode_to_bytes(sent.text) for sent in self.spacy_doc.sents] for ext in ('.txt', '.gz', '.bz2', '.xz'): filename = os.path.join( self.tempdir, 'test_read_write_file_lines_bytes' + ext) fileio.write_file_lines(expected, filename, mode='wb', auto_make_dirs=True) observed = [line.strip() for line in fileio.read_file_lines(filename, mode='rb')] self.assertEqual(observed, expected)
def test_read_write_file_lines_bz2(self): expected = [sent.text for sent in self.spacy_doc.sents] filename = os.path.join(self.tempdir, 'test_read_write_file_lines.txt.bz2') fileio.write_file_lines(expected, filename, mode='wb' if PY2 else 'wt') observed = [ line.strip() for line in fileio.read_file_lines(filename, mode='r' if PY2 else 'rt') ] self.assertEqual(observed, expected)
def test_read_write_file_lines_unicode(self): expected = [sent.text for sent in self.spacy_doc.sents] for ext in ('.txt', '.gz', '.bz2', '.xz'): filename = os.path.join( self.tempdir, 'test_read_write_file_lines_unicode' + ext) if PY2 is True and ext != '.txt': self.assertRaises( ValueError, fileio.open_sesame, filename, 'wt', None, True) else: fileio.write_file_lines(expected, filename, mode='wt', auto_make_dirs=True) observed = [line.strip() for line in fileio.read_file_lines(filename, mode='rt')] self.assertEqual(observed, expected)
def test_read_write_file_lines_bytes(self): expected = [ unicode_to_bytes(sent.text) for sent in self.spacy_doc.sents ] for ext in ('.txt', '.gz', '.bz2', '.xz'): filename = os.path.join(self.tempdir, 'test_read_write_file_lines_bytes' + ext) if is_python2 is True and ext == '.xz': self.assertRaises(ValueError, fileio.open_sesame, filename, 'wb', 'utf-8', True) else: fileio.write_file_lines(expected, filename, mode='wb', auto_make_dirs=True) observed = [ line.strip() for line in fileio.read_file_lines(filename, mode='rb') ] self.assertEqual(observed, expected)