IOSpec types#

BaseIOSpec#

class BaseIOSpec[source]#

Abstract base class for storing objects in files.

This base class is inherited from by IOSpec classes, such as PandasData and ModuleData, and defines properties shared among the child classes. The Model.get_spec method returns the IOSpec object associated with a given object referenced in the model. Model.iospecs returns a list of all the IOSpec associated with the objects referenced in the model.

Example

The code below returns the PandasData object associated with a DataFrame df referenced in the model:

>>> model.get_spec(df)
<PandasData path='df.xlsx' file_type='excel' sheet='df1'>

The code below returns a list of all the IOSpec objects associated with the objects referenced in the model:

>>> model.iospecs
[<PandasData path='df.xlsx' file_type='excel' sheet='df1'>,
 <PandasData path='df.xlsx' file_type='excel' sheet='s1'>,
 <ModuleData path='mod1.py'>]

Changed in version 0.20.0: Renamed from BaseDataSpec to BaseIOSpec

Changed in version 0.18.0: The is_hidden parameter is removed.

Changed in version 0.18.0: Renamed from BaseDataClient to BaseDataSpec.

property BaseIOSpec.path#

File path that the object is written to.

This property is defined in BaseIOSpec, and returns the path to a file to which the object is written to when the model is saved by the write() method. The returned path is an instance of the Path class defined in pathlib module in the Python standard library. The Path class is either WindowsPath or PosixPath, depending on the platform the Python session is running on.

This property also works as a setter to set the path. str or any path-like object can be given.

Example

In the code below, df is a DataFrame referenced in model, and the IPython sessin is running on Windows. The DataFrame is saved in an Excel file named df.xlsx in the model folder:

>>> model.get_spec(df).path
WindowsPath('df.xlsx')

The next assignment changes the file path to files/df2.xlsx under the model folder:

>>> model.get_spec(df).path = 'files/df2.xlsx'

>>> model.get_spec(df).path
WindowsPath('files/df.xlsx')

PandasData#

class PandasData(data, sheet=None)[source]#

A subclass of BaseIOSpec that associates a pandas DataFrame or Series with a file

A PandasData holds a pandas DataFrame or Series object, and associates it with a file for writing and reading the object.

A PandasData can be created only by UserSpace.new_pandas or Model.new_pandas.

The DataFrame or Series held in PandasData objects are accessible through value property or a call () method.

Parameters:
  • path – Path to a file for saving data. If a relative path is given, it is relative to the model folder.

  • data – a pandas DataFrame or Series.

  • filetype (str) – String to specify the file format. “excel” or “csv”

path#

A path to the associated file as a pathlib.Path object. See BaseIOSpec.path.

filetype#

“excel” or “csv”.

Type:

str

Changed in version 0.18.0: The expose_data parameter is removed.

property PandasData.value#

pandas DataFrame or Series held in the object

property PandasData.sheet#

The name of the sheet to which the data is written to

ModuleData#

class ModuleData(module=None)[source]#

A subclass of BaseIOSpec that associates a user module with its source file in the model

A ModuleData is created either by UserSpace.new_module or Model.new_module when a user module is assigned to a Reference. The ModuleData is assigned to the _mx_dataclient attribute of the module.

New in version 0.13.0.

property ModuleData.value#

Module held in the object

ExcelRange#

class ExcelRange(range_, sheet=None, keyids=None)[source]#

Mapping class for accessing Excel ranges

An ExcelRange is a dict-like object that represents a range in an Excel file. The user can read values from the range or write values to it by the subscription operator []. ExcelRange is a mapping class, thus it implements all the mapping methods and operations.

ExcelRange objects can only be created by the Model.new_excel_range or UserSpace.new_excel_range method.

ExcelRange is a subclass of the BaseIOSpec abstract class. The iospecs property list all the BaseIOSpec instances held in the Model including ExcelRange objects.

New in version 0.9.0.