openers¶
Context manager openers for various fileobject types
|
|
|
Opener-type class to collect extra compressed extensions |
|
Class to accept, maybe open, and context-manage file-likes / filenames |
Fileish¶
ImageOpener¶
- class nibabel.openers.ImageOpener(fileish: str | IOBase, *args, **kwargs)¶
Bases:
OpenerOpener-type class to collect extra compressed extensions
A trivial sub-class of opener to which image classes can add extra extensions with custom openers, such as compressed openers.
To add an extension, add a line to the class definition (not __init__):
ImageOpener.compress_ext_map[ext] = func_def
extis a file extension beginning with ‘.’ and should be included in the image class’svalid_extstuple.func_defis a (function, (args,)) tuple, where function accepts a filename as the first parameter, and `args defines the other arguments that function accepts. These arguments must be any (unordered) subset of mode, compresslevel, and buffering.- __init__(fileish: str | IOBase, *args, **kwargs)¶
- compress_ext_map: dict[str | None, OpenerDef] = {'.bz2': (<class 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel')), '.gz': (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open')), '.mgz': (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open')), '.zst': (<function zstd_open>, ('mode', 'level', 'option', 'zstd_dict')), None: (<built-in function open>, ('mode', 'buffering'))}¶
Opener¶
- class nibabel.openers.Opener(fileish: str | IOBase, *args, **kwargs)¶
Bases:
objectClass to accept, maybe open, and context-manage file-likes / filenames
Provides context manager to close files that the constructor opened for you.
- Parameters:
- fileishstr or file-like
if str, then open with suitable opening method. If file-like, accept as is
- *argspositional arguments
passed to opening method when fileish is str.
mode, if not specified, is rb.compresslevel, if relevant, and not specified, is set from class variabledefault_compresslevel.keep_open, if relevant, and not specified, isFalse.- **kwargskeyword arguments
passed to opening method when fileish is str. Change of defaults as for *args
- __init__(fileish: str | IOBase, *args, **kwargs)¶
- bz2_def = (<class 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel'))¶
- close() None¶
- close_if_mine() None¶
Close
self.fobjiff we opened it in the constructor
- property closed: bool¶
- compress_ext_icase: bool = True¶
whether to ignore case looking for compression extensions
- compress_ext_map: dict[str | None, OpenerDef] = {'.bz2': (<class 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel')), '.gz': (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open')), '.zst': (<function zstd_open>, ('mode', 'level', 'option', 'zstd_dict')), None: (<built-in function open>, ('mode', 'buffering'))}¶
- default_compresslevel = 1¶
default compression level when writing gz and bz2 files
- default_zst_level = {'r': None, 'rb': None, 'w': 3, 'wb': 3}¶
default compression level for zst files
- fileno() int¶
- fobj: io.IOBase¶
- gz_def = (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open'))¶
- property mode: str¶
- property name: str | None¶
Return
self.fobj.nameor self._name if not presentself._name will be None if object was created with a fileobj, otherwise it will be the filename.
- read(size: int = -1, /) bytes¶
- readinto(buffer: WriteableBuffer, /) int | None¶
- seek(pos: int, whence: int = 0, /) int¶
- tell() int¶
- write(b: bytes, /) int | None¶
- writelines(lines: ty.Iterable[ReadableBuffer], /) None¶
- zstd_def = (<function zstd_open>, ('mode', 'level', 'option', 'zstd_dict'))¶