| Conditions | 6 |
| Total Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| 1 | """Low-level utilities for reading a variety of source formats.""" |
||
| 9 | def open_(filename, mode='r', encoding=None): |
||
| 10 | """Open a text file with encoding and optional gzip compression. |
||
| 11 | |||
| 12 | This method maintains picklability whenever possible, and ensure GZip |
||
| 13 | is handled efficiently. |
||
| 14 | |||
| 15 | Parameters |
||
| 16 | ---------- |
||
| 17 | filename : str |
||
| 18 | The filename to read. |
||
| 19 | mode : str, optional |
||
| 20 | The mode with which to open the file. Defaults to `r`. |
||
| 21 | encoding : str, optional |
||
| 22 | The encoding to use (see the codecs documentation_ for supported |
||
| 23 | values). Defaults to ``None``. |
||
| 24 | |||
| 25 | .. _documentation: |
||
| 26 | https://docs.python.org/3/library/codecs.html#standard-encodings |
||
| 27 | |||
| 28 | """ |
||
| 29 | if filename.endswith('.gz'): |
||
| 30 | if six.PY2: |
||
| 31 | zf = io.BufferedReader(gzip.open(filename, mode)) |
||
| 32 | if encoding: |
||
| 33 | return codecs.getreader(encoding)(zf) |
||
| 34 | else: |
||
| 35 | return zf |
||
| 36 | else: |
||
| 37 | return io.BufferedReader(gzip.open(filename, mode, |
||
| 38 | encoding=encoding)) |
||
| 39 | if six.PY2: |
||
| 40 | if encoding: |
||
| 41 | return codecs.open(filename, mode, encoding=encoding) |
||
| 42 | else: |
||
| 43 | return open(filename, mode) |
||
| 44 | else: |
||
| 45 | return open(filename, mode, encoding=encoding) |
||
| 46 | |||
| 66 |