pyrokinetics.file_utils.AbstractFileReader#

class pyrokinetics.file_utils.AbstractFileReader[source]#

Bases: ABC

An abstract base class for classes that can read data from disk and create a Pyrokinetics object. Subclasses should define both a read_from_file method and a verify_file_type method.

Subclasses should usually make use of FileReader, as this additionally associates the class with its associated ‘readable’. These classes are kept separate to handle the special case of GKInput, as it is both the ‘readable’ class and the reader.

__init__()#

Methods

__init__()

read_from_file(filename, *args, **kwargs)

Read and process the data from a file.

verify_file_type(filename)

Perform a series of checks on the file to ensure it is valid.

abstract read_from_file(filename, *args, **kwargs)[source]#

Read and process the data from a file.

Parameters:
  • filename (PathLike) – The file to be read.

  • *args – Additional positional arguments used by the derived file reader.

  • **kwargs – Keyword arguments used by the derived file reader.

Returns:

Derived classes may return any type of data from this function.

Return type:

Any

Notes

Rather than accepting *args and/or **kwargs, it is recommended that derived classes should specify their keywords explicitly.

verify_file_type(filename)[source]#

Perform a series of checks on the file to ensure it is valid. Raises an exception if the file is of the wrong type. Exits normally if the file is valid.

The default implementation simply reads the file, performs the usual processing, and discards the results. This is rarely the best way to verify a file type, so this should be overridden is most cases. In particular, the default implementation should not be used if:

  • Reading and processing the whole file is computationally expensive.

  • The read function depends upon keyword arguments.

  • The read function can read multiple related file types and further information is needed to differentiate between them. For example, multiple gyrokinetics codes use Fortran namelists as input files, so a specialised verify method is needed to check the names stored within to determine which code the input file belongs to.

  • An exception raised when reading from file should halt the program.

Parameters:

filename (PathLike) – The file to be read.

Return type:

None