msl.io.base_io module
Base classes for all Readers. and Writers.
- class msl.io.base.Reader(file)[source]
Bases:
Root- static can_read(file, **kwargs)[source]
Whether this
Readercan read the file specified by file.Important
You must override this method.
- static get_bytes(file, *args)[source]
Return bytes from a file.
- Parameters:
file (path-like or file-like) – The file to read bytes from.
*args (
intortupleofint, optional) –The position(s) in the file to retrieve bytes from.
Examples:
get_bytes(file)\(\rightarrow\) returns all bytesget_bytes(file, 5)\(\rightarrow\) returns the first 5 bytesget_bytes(file, -5)\(\rightarrow\) returns the last 5 bytesget_bytes(file, 5, 10)\(\rightarrow\) returns bytes 5 through 10 (inclusive)get_bytes(file, 3, -1)\(\rightarrow\) skips the first 2 bytes and returns the restget_bytes(file, -8, -4)\(\rightarrow\) returns the eighth- through fourth-last bytes (inclusive)get_bytes(file, 1, -1, 2)\(\rightarrow\) returns every other byte
- Returns:
bytes– The bytes from the file.
- static get_lines(file, *args, **kwargs)[source]
Return lines from a file.
- Parameters:
file (path-like or file-like) – The file to read lines from.
*args (
intortupleofint, optional) –The line(s) in the file to get.
Examples:
get_lines(file)\(\rightarrow\) returns all linesget_lines(file, 5)\(\rightarrow\) returns the first 5 linesget_lines(file, -5)\(\rightarrow\) returns the last 5 linesget_lines(file, 2, 4)\(\rightarrow\) returns lines 2, 3 and 4get_lines(file, 4, -1)\(\rightarrow\) skips the first 3 lines and returns the restget_lines(file, 2, -2)\(\rightarrow\) skips the first and last lines and returns the restget_lines(file, -4, -2)\(\rightarrow\) returns the fourth-, third- and second-last linesget_lines(file, 1, -1, 6)\(\rightarrow\) returns every sixth line in the file
**kwargs –
remove_empty_lines:boolWhether to remove all empty lines. Default is
False.encoding:strThe name of the encoding to use to decode the file. The default is
'utf-8'.errors:strAn optional string that specifies how encoding errors are to be handled. Either
'strict'or'ignore'. The default is'strict'.
- Returns:
listofstr– The lines from the file. Trailing whitespace is stripped from each line.
- class msl.io.base.Root(file, **metadata)[source]
Bases:
Group- Parameters:
- tree(indent=2)[source]
A representation of the tree structure of all
Groups andDatasets that are inRoot.- Parameters:
indent (
int, optional) – The amount of indentation to add for each recursive level.- Returns:
str– The tree structure.
- class msl.io.base.Writer(file=None, **metadata)[source]
Bases:
Root- Parameters:
- update_context_kwargs(**kwargs)[source]
When a
Writeris used as a context manager thewrite()method is automatically called when exiting the context manager. You can specify the keyword arguments that will be passed to thewrite()method by callingupdate_context_kwargs()with the appropriate key-value pairs before the context manager exits. You can call this method multiple times since the key-value pairs get added to the underlyingdict(viadict.update()) that contains all keyword arguments which are passed to thewrite()method.
- write(file=None, root=None, **kwargs)[source]
Write to a file.
Important
You must override this method.
- Parameters:
file (path-like or file-like, optional) – The file to write the root to. If
Nonethen uses the file value that was specified when theWriterwas instantiated.root (
Root, optional) – Write the root object in the file format of thisWriter. This is useful when converting between different file formats.**kwargs – Additional key-value pairs to use when writing the file.