Model

class modelx.core.model.Model[source]

Bases: modelx.core.spacecontainer.SpaceContainer

Top-level container in modelx object hierarchy.

Model instances are the top-level objects and directly contain Space objects, which in turn contain other spaces or Cells objects.

A model can be created by new_model API function.

Methods

close() Close the model.
cur_space([name]) Set the current space to Space name and return it.
import_module(module_[, recursive]) Create a child space from an module.
new_space([name, bases, formula, refs]) Create a child space.
new_space_from_excel(book, range_[, sheet, …]) Create a child space from an Excel range.
new_space_from_module(module_[, recursive]) Create a child space from an module.
rename(name) Rename the model itself
save(filepath) Save the model to a file.

Attributes

allow_none Whether a cells can have None as its value.
cellgraph A directed graph of cells.
fullname Dotted name of the object.
model The model this object belongs to.
name Name of the object.
parent The parent of this object.
properties
refs Return a mapping of global references.
spaces A mapping of the names of child spaces to the Space objects
rename(name)[source]

Rename the model itself

save(filepath)[source]

Save the model to a file.

close()[source]

Close the model.

cellgraph

A directed graph of cells.

refs

Return a mapping of global references.

allow_none

Whether a cells can have None as its value.

This is a property of Model, Space and Cells. If allow_none of a cells is False, the cells cannot have None as its value. Assigning None to the cells or its formula returning None raises an Error. If True, the cells can have None as their value. If set to None, allow_none of its parent is looked up, and the search continues until True or False is found.

Returns:True if the cells can have None, False if it cannot, or None if a default value from the parent is to be used.
cur_space(name=None)

Set the current space to Space name and return it.

If called without arguments, the current space is returned. Otherwise, the current space is set to the space named name and the space is returned.

fullname

Dotted name of the object.

Names joined by dots, such as ‘Model1.Space1.Cells1’, each element in the string is the name of the parent object of the next one joined by a dot.

import_module(module_, recursive=False, **params)

Create a child space from an module.

Parameters:
  • module – a module object or name of the module object.
  • recursive – Not yet implemented.
  • **params – arguments to pass to new_space
Returns:

The new child space created from the module.

model

The model this object belongs to.

This is a property of Model, Space and Cells. For models, this property is themselves.

name

Name of the object.

new_space(name=None, bases=None, formula=None, refs=None)

Create a child space.

Parameters:
  • name (str, optional) – Name of the space. Defaults to SpaceN, where N is a number determined automatically.
  • bases (optional) – A space or a sequence of spaces to be the base space(s) of the created space.
  • formula (optional) – Function to specify the parameters of dynamic child spaces. The signature of this function is used for setting parameters for dynamic child spaces. This function should return a mapping of keyword arguments to be passed to this method when the dynamic child spaces are created.
Returns:

The new child space.

new_space_from_excel(book, range_, sheet=None, name=None, names_row=None, param_cols=None, space_param_order=None, cells_param_order=None, transpose=False, names_col=None, param_rows=None)

Create a child space from an Excel range.

To use this method, openpyxl package must be installed.

Parameters:
  • book (str) – Path to an Excel file.
  • range (str) – Range expression, such as “A1”, “$G4:$K10”, or named range “NamedRange1”.
  • sheet (str) – Sheet name (case ignored).
  • name (str, optional) – Name of the space. Defaults to SpaceN, where N is a number determined automatically.
  • names_row (optional) – an index number indicating what row contains the names of cells and parameters. Defaults to the top row (0).
  • param_cols (optional) – a sequence of index numbers indicating parameter columns. Defaults to only the leftmost column ([0]).
  • names_col (optional) – an index number, starting from 0, indicating what column contains additional parameters.
  • param_rows (optional) – a sequence of index numbers, starting from 0, indicating rows of additional parameters, in case cells are defined in two dimensions.
  • transpose (optional) – Defaults to False. If set to True, “row(s)” and “col(s)” in the parameter names are interpreted inversely, i.e. all indexes passed to “row(s)” parameters are interpreted as column indexes, and all indexes passed to “col(s)” parameters as row indexes.
  • space_param_order – a sequence to specify space parameters and their orders. The elements of the sequence denote the indexes of param_cols elements, and optionally the index of param_rows elements shifted by the length of param_cols. The elements of this parameter and cell_param_order must not overlap.
  • cell_param_order (optional) – a sequence to reorder the parameters. The elements of the sequence denote the indexes of param_cols elements, and optionally the index of param_rows elements shifted by the length of param_cols. The elements of this parameter and cell_space_order must not overlap.
Returns:

The new child space created from the Excel range.

new_space_from_module(module_, recursive=False, **params)

Create a child space from an module.

Alias to import_module().

Parameters:
  • module – a module object or name of the module object.
  • recursive – Not yet implemented.
  • **params – arguments to pass to new_space
Returns:

The new child space created from the module.

parent

The parent of this object. None for models.

The parent object of a cells is a space that contains the cells. The parent object of a space is either a model or another space that contains the space.

spaces

A mapping of the names of child spaces to the Space objects