fastoad.io package

Subpackages

Submodules

Module contents

Package for handling input/output streams

class fastoad.io.DataFile(data_source: str | PathLike | IO | list | None = None, formatter: IVariableIOFormatter | None = None, load_data: bool = True)[source]

Bases: VariableList

Class for managing FAST-OAD data files.

Behaves like VariableList class but has load() and save() methods.

If variable list is specified for data_source, file_path will have to be set before using :method:`save`.

Parameters:
  • data_source – Can be the file path where data will be loaded and saved, or a list of Variable instances that will be used for initialization (or a VariableList instance).

  • formatter – (ignored if data_source is not an I/O stream nor a file path) a class that determines the file format to be used. Defaults to FAST-OAD native format. See VariableIO for more information.

  • load_data – (ignored if data_source is not an I/O stream nor a file path) if True, file is expected to exist and its content will be loaded at instantiation.

property file_path: str

Path of data file.

property formatter: IVariableIOFormatter

Class that defines the file format.

load()[source]

Loads file content.

save()[source]

Saves current state of variables in file.

save_as(file_path: str | PathLike, formatter: IVariableIOFormatter | None = None, overwrite=False)[source]

Sets the associated file path as specified and saves current state of variables.

Parameters:
  • file_path

  • formatter – a class that determines the file format to be used. Defaults to FAST-OAD native format. See VariableIO for more information.

  • overwrite – if specified file already exists and overwrite is False, an error is triggered.

class fastoad.io.IVariableIOFormatter[source]

Bases: ABC

Interface for formatter classes to be used in VariableIO class.

The file format is defined by the implementation of this interface.

abstractmethod read_variables(data_source: str | PathLike | IO) VariableList[source]

Reads variables from provided data source file.

Parameters:

data_source

Returns:

a list of Variable instance

abstractmethod write_variables(data_source: str | PathLike | IO, variables: VariableList)[source]

Writes variables to defined data source file.

Parameters:
  • data_source

  • variables

class fastoad.io.VariableIO(data_source: str | PathLike | IO | None, formatter: IVariableIOFormatter | None = None)[source]

Bases: object

Class for reading and writing variable values from/to file.

The file format is defined by the class provided as formatter argument.

Parameters:
  • data_source – I/O stream, or file path, used for reading or writing data

  • formatter – a class that determines the file format to be used. Defaults to a VariableBasicXmlFormatter instance.

data_source

I/O stream, or file path, used for reading or writing data

property formatter: IVariableIOFormatter

Class that determines the file format to be used.

read(only: list[str] | None = None, ignore: list[str] | None = None) VariableList[source]

Reads variables from provided data source.

Elements of only and ignore can be real variable names or Unix-shell-style patterns. In any case, comparison is case-sensitive.

Parameters:
  • only – List of variable names that should be read. Other names will be ignored. If None, all variables will be read.

  • ignore – List of variable names that should be ignored when reading.

Returns:

a VariableList instance where outputs have been defined using provided source.

write(variables: VariableList, only: list[str] | None = None, ignore: list[str] | None = None)[source]

Writes variables from provided VariableList instance.

Elements of only and ignore can be real variable names or Unix-shell-style patterns. In any case, comparison is case-sensitive.

Parameters:
  • variables – a VariableList instance

  • only – List of variable names that should be written. Other names will be ignored. If None, all variables will be written.

  • ignore – List of variable names that should be ignored when writing