| Conditions | 2 |
| Total Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| 1 | """Low-level utilities for reading a variety of source formats.""" |
||
| 9 | def open_(filename, mode='r', encoding='utf-8'): |
||
| 10 | """Open a text file with UTF-8 and optional gzip compression. |
||
| 11 | |||
| 12 | This function is useful when dealing with text files. |
||
| 13 | |||
| 14 | Parameters |
||
| 15 | ---------- |
||
| 16 | filename : str |
||
| 17 | The filename to read. |
||
| 18 | mode : str, optional |
||
| 19 | The mode with which to open the file. Defaults to `r`. |
||
| 20 | encoding : str, optional |
||
| 21 | The encoding to use (see the codecs documentation_ for supported |
||
| 22 | values). Defaults to `utf-8`. |
||
| 23 | |||
| 24 | .. _documentation: |
||
| 25 | https://docs.python.org/3/library/codecs.html#standard-encodings |
||
| 26 | |||
| 27 | """ |
||
| 28 | if filename.endswith('.gz.'): |
||
| 29 | zf = io.BufferedReader(gzip.open(filename, mode)) |
||
| 30 | return codecs.getreader(encoding)(zf) |
||
| 31 | return codecs.open(filename, mode, encoding=encoding) |
||
| 32 | |||
| 52 |