1414## fgpyo.io Examples:
1515
1616```python
17+ >>> import fgpyo.io as fio
18+ >>> from fgpyo.io import write_lines, read_lines
19+ >>> from pathlib import Path
20+
21+ ```
22+
23+ Assert that a path exists and is readable:
24+
25+ ```python
26+ >>> tmp_dir = Path(getfixture("tmp_path"))
27+ >>> path_flat: Path = tmp_dir / "example.txt"
28+ >>> fio.assert_path_is_readable(path_flat) # doctest: +ELLIPSIS
29+ Traceback (most recent call last):
30+ ...
31+ AssertionError: Cannot read non-existent path: ...
32+
33+ ```
34+
35+ Write to and read from path:
36+
37+ ```python
38+ >>> path_flat = tmp_dir / "example.txt"
39+ >>> path_compressed = tmp_dir / "example.txt.gz"
40+ >>> write_lines(path=path_flat, lines_to_write=["flat file", 10])
41+ >>> write_lines(path=path_compressed, lines_to_write=["gzip file", 10])
42+
43+ ```
44+
45+ Read lines from a path into a generator:
46+
47+ ```python
48+ >>> lines = read_lines(path=path_flat)
49+ >>> next(lines)
50+ 'flat file'
51+ >>> next(lines)
52+ '10'
53+ >>> lines = read_lines(path=path_compressed)
54+ >>> next(lines)
55+ 'gzip file'
56+ >>> next(lines)
57+ '10'
1758
18- >>> import fgpyo.io as fio
19- >>> from pathlib import Path
20- Assert that a path exists and is readable
21- >>> path_flat: Path = Path("example.txt")
22- >>> path_compressed: Path = Path("example.txt.gz")
23- >>> fio.path_is_readable(path_flat)
24- AssertionError: Cannot read non-existent path: example.txt
25- >>> fio.path_is_readable(compressed_file)
26- AssertionError: Cannot read non-existent path: example.txt.gz
27- Write to and read from path
28- >>> write_lines(path = path_flat, lines_to_write=["flat file", 10])
29- >>> write_lines(path = path_compressed, lines_to_write=["gzip file", 10])
30- Read lines from a path into a generator
31- >>> lines = read_lines(path = path_flat)
32- >>> next(lines)
33- "flat file"
34- >>> next(lines)
35- "10"
36- >>> lines = read_lines(path = path_compressed)
37- >>> next(lines)
38- "gzip file"
39- >>> next(lines)
40- "10"
4159```
4260
4361"""
@@ -165,9 +183,10 @@ def to_reader(path: Path, threads: Optional[int] = None) -> TextIOWrapper:
165183 threads: the number of threads to use when decompressing gzip files
166184
167185 Example:
168- >>> reader = fio.to_reader(path = Path("reader.txt"))
169- >>> reader.readlines()
170- >>> reader.close()
186+ >>> import fgpyo.io as fio
187+ >>> reader = fio.to_reader(path=Path("reader.txt")) # doctest: +SKIP
188+ >>> reader.readlines() # doctest: +SKIP
189+ >>> reader.close() # doctest: +SKIP
171190
172191 """
173192 if path .suffix in COMPRESSED_FILE_EXTENSIONS :
@@ -189,9 +208,10 @@ def to_writer(path: Path, append: bool = False, threads: Optional[int] = None) -
189208 threads: the number of threads to use when compressing gzip files
190209
191210 Example:
192- >>> writer = fio.to_writer(path = Path("writer.txt"))
193- >>> writer.write(f'{something}\\ n')
194- >>> writer.close()
211+ >>> import fgpyo.io as fio
212+ >>> writer = fio.to_writer(path=Path("writer.txt")) # doctest: +SKIP
213+ >>> writer.write("something\\ n") # doctest: +SKIP
214+ >>> writer.close() # doctest: +SKIP
195215
196216 """
197217 mode_prefix : str = "a" if append else "w"
@@ -226,7 +246,9 @@ def read_lines(path: Path, strip: bool = False, threads: Optional[int] = None) -
226246 threads: the number of threads to use when decompressing gzip files
227247
228248 Example:
229- read_back = fio.read_lines(path)
249+ >>> import fgpyo.io as fio
250+ >>> read_back = fio.read_lines(path) # doctest: +SKIP
251+
230252 """
231253 with to_reader (path = path , threads = threads ) as reader :
232254 if strip :
0 commit comments