msl.io.base_io module

Base classes for all Readers. and Writers.

class msl.io.base.Reader(file)[source]

Bases: Root

Parameters:

file (path-like or file-like) – The file to read.

static can_read(file, **kwargs)[source]

Whether this Reader can read the file specified by file.

Important

You must override this method.

Parameters:
  • file (path-like or file-like) – The file to check whether the Reader can read it.

  • **kwargs – Key-value pairs that the Reader class may need when checking if it can read the file.

Returns:

bool – Either True (can read) or False (cannot read).

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 (int or tuple of int, optional) –

    The position(s) in the file to retrieve bytes from.

    Examples:

    • get_bytes(file) \(\rightarrow\) returns all bytes

    • get_bytes(file, 5) \(\rightarrow\) returns the first 5 bytes

    • get_bytes(file, -5) \(\rightarrow\) returns the last 5 bytes

    • get_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 rest

    • get_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_extension(file)[source]

Return the extension of the file.

Parameters:

file (path-like or file-like) – The file to get the extension of.

Returns:

str – The extension, including the '.'.

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 (int or tuple of int, optional) –

    The line(s) in the file to get.

    Examples:

    • get_lines(file) \(\rightarrow\) returns all lines

    • get_lines(file, 5) \(\rightarrow\) returns the first 5 lines

    • get_lines(file, -5) \(\rightarrow\) returns the last 5 lines

    • get_lines(file, 2, 4) \(\rightarrow\) returns lines 2, 3 and 4

    • get_lines(file, 4, -1) \(\rightarrow\) skips the first 3 lines and returns the rest

    • get_lines(file, 2, -2) \(\rightarrow\) skips the first and last lines and returns the rest

    • get_lines(file, -4, -2) \(\rightarrow\) returns the fourth-, third- and second-last lines

    • get_lines(file, 1, -1, 6) \(\rightarrow\) returns every sixth line in the file

  • **kwargs

    • remove_empty_lines : bool

      Whether to remove all empty lines. Default is False.

    • encoding : str

      The name of the encoding to use to decode the file. The default is 'utf-8'.

    • errors : str

      An optional string that specifies how encoding errors are to be handled. Either 'strict' or 'ignore'. The default is 'strict'.

Returns:

list of str – The lines from the file. Trailing whitespace is stripped from each line.

read(**kwargs)[source]

Read the file.

The file can be accessed by the file property of the Reader, i.e., self.file

Important

You must override this method.

Parameters:

**kwargs – Key-value pairs that the Reader class may need when reading the file.

class msl.io.base.Root(file, **metadata)[source]

Bases: Group

The root vertex in a tree.

Parameters:
property file

The file object that is associated with the Root.

Type:

path-like or file-like

tree(indent=2)[source]

A representation of the tree structure of all Groups and Datasets that are in Root.

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:
  • file (path-like or file-like, optional) – The file to write the data to. Can also be specified in the write() method.

  • **metadata – Key-value pairs that are used as Metadata of the Root.

save(file=None, root=None, **kwargs)[source]

Alias for write().

set_root(root)[source]

Set a new Root for the Writer.

Attention

This will clear the Metadata of the Writer and all Groups and Datasets that the Writer currently contains. The file that was specified when the Writer was created does not change.

Parameters:

root (Root) – The new Root for the Writer.

update_context_kwargs(**kwargs)[source]

When a Writer is used as a context manager the write() method is automatically called when exiting the context manager. You can specify the keyword arguments that will be passed to the write() method by calling update_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 underlying dict (via dict.update()) that contains all keyword arguments which are passed to the write() 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 None then uses the file value that was specified when the Writer was instantiated.

  • root (Root, optional) – Write the root object in the file format of this Writer. This is useful when converting between different file formats.

  • **kwargs – Additional key-value pairs to use when writing the file.