def test_to_file_open(self): tmp_dir = Path(tempfile.mkdtemp()) file_path = tmp_dir / "test.txt" with CharSink.to_file(file_path).open() as out: out.write("hello\n\nworld\n") source = CharSource.from_file(file_path) self.assertEqual("hello\n\nworld\n", source.read_all()) shutil.rmtree(str(tmp_dir))
def test_to_file_write_string_arg(self): tmp_dir = Path(tempfile.mkdtemp()) file_path = tmp_dir / "test.txt" sink = CharSink.to_file(str(file_path)) sink.write("hello\n\nworld\n") source = CharSource.from_file(str(file_path)) self.assertEqual("hello\n\nworld\n", source.read_all()) shutil.rmtree(str(tmp_dir))
def binary_from_doc_id_to_file_map( map_file: Union[str, Path, CharSource] ) -> "KeyValueSource[str, bytes]": if not isinstance(map_file, CharSource): map_file = CharSource.from_file(map_file) return _PathMappingBytesKeyValueSource( # type: ignore read_doc_id_to_file_map(map_file) )
def test_from_file(self): source = CharSource.from_file( Path(__file__).parent / "char_source_test.txt") self.assertEqual("Hello\nworld\n", source.read_all()) self.assertEqual(["Hello", "world"], source.readlines()) self.assertFalse(source.is_empty()) with source.open() as inp: self.assertEqual("Hello\n", inp.readline()) self.assertEqual("world\n", inp.readline())
def __getitem__(self, key: str) -> str: return CharSource.from_file(self.id_to_path[key]).read_all()