Space

class modelx.core.space.Space[source]

Bases: modelx.core.spacecontainer.SpaceContainer

Container of cells, other spaces, and cells namespace.

Space objects can contain cells and other spaces. Spaces have mappings of names to objects that serve as global namespaces of the formulas of the cells in the spaces.

Methods

add_bases(*bases) Add base spaces.
cur_space([name]) Set the current space to Space name and return it.
has_params() Check if the parameter function is set.
import_funcs(module_) Create a cells from a module.
import_module(module_[, recursive]) Create a child space from an module.
in_dynamic() True if the space is in a dynamic space, False otherwise.
is_base(other) True if the space is a base space of other, False otherwise.
is_defined() True if the space is a defined space, False otherwise.
is_derived() True if the space is a derived space, False otherwise.
is_dynamic() True if ths space is a dynamic space, False otherwise.
is_static() True if the space is a static space, False if dynamic.
is_sub(other) True if the space is a sub space of other, False otherwise.
new_cells([name, formula]) Create a cells in the space.
new_cells_from_excel(book, range_[, sheet, …]) Create multiple cells from an Excel range.
new_cells_from_module(module_) Create a cells from a 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.
reload() Reload the source module and update the formulas.
remove_bases(*bases) Remove base spaces.
set_formula(formula) Set if the parameter function.
to_frame(*args) Convert the space itself into a Pandas DataFrame object.

Attributes

all_spaces A mapping associating names to all(static and dynamic) spaces.
allow_none Whether a cells can have None as its value.
argvalues A tuple of space arguments.
bases List of base classes.
cells A mapping of cells names to the cells objects in the space.
derived_cells A mapping associating names to derived cells.
derived_spaces A mapping associating names to derived spaces.
dynamic_spaces A mapping associating names to dynamic spaces.
formula Property to get, set, delete formula.
frame Alias of to_frame().
fullname Dotted name of the object.
model The model this object belongs to.
name Name of the object.
parameters A tuple of parameter strings.
parent The parent of this object.
properties
refs A map associating names to objects accessible by the names.
self_cells A mapping that associates names to cells defined in the space
self_spaces A mapping associating names to self spaces.
spaces A mapping associating names to static spaces.
static_spaces A mapping associating names to static spaces.
new_cells(name=None, formula=None)[source]

Create a cells in the space.

Parameters:
  • name – If omitted, the model is named automatically CellsN, where N is an available number.
  • func – The function to define the formula of the cells.
Returns:

The new cells.

add_bases(*bases)[source]

Add base spaces.

remove_bases(*bases)[source]

Remove base spaces.

bases

List of base classes.

is_base(other)[source]

True if the space is a base space of other, False otherwise.

is_sub(other)[source]

True if the space is a sub space of other, False otherwise.

is_static()[source]

True if the space is a static space, False if dynamic.

is_derived()[source]

True if the space is a derived space, False otherwise.

is_defined()[source]

True if the space is a defined space, False otherwise.

is_dynamic()[source]

True if ths space is a dynamic space, False otherwise.

in_dynamic()[source]

True if the space is in a dynamic space, False otherwise.

cells

A mapping of cells names to the cells objects in the space.

self_cells

A mapping that associates names to cells defined in the space

derived_cells

A mapping associating names to derived cells.

all_spaces

A mapping associating names to all(static and dynamic) spaces.

spaces

A mapping associating names to static spaces.

static_spaces

A mapping associating names to static spaces.

Alias to spaces()

dynamic_spaces

A mapping associating names to dynamic spaces.

self_spaces

A mapping associating names to self spaces.

derived_spaces

A mapping associating names to derived spaces.

argvalues

A tuple of space arguments.

parameters

A tuple of parameter strings.

refs

A map associating names to objects accessible by the names.

import_funcs(module_)[source]

Create a cells from a module.

new_cells_from_module(module_)[source]

Create a cells from a module.

Alias to import_funcs().

reload()[source]

Reload the source module and update the formulas.

If the space was created from a module, reload the module and update the formulas of its cells.

If a cell in the space is not created from a function definition in the source module of the space, it is not updated.

If the formula of a cell in the space was created from a function definition in the source module of the space and the definition is missing from the updated module, the formula is cleared and values calculated directly or indirectly depending the cells are cleared.

If the formula of a cell in the space has not been changed before and after reloading the source module, the values held in the cell and relevant cells are retained.

Returns:This method returns the space itself.
new_cells_from_excel(book, range_, sheet=None, names_row=None, param_cols=None, param_order=None, transpose=False, names_col=None, param_rows=None)[source]

Create multiple cells from an Excel range.

This method reads values from a range in an Excel file, create cells and populate them with the values in the range. To use this method, openpyxl package must be installed.

The Excel file to read data from is specified by book parameters. The range_ can be a range address, such as “$G4:$K10”, or a named range. In case a range address is given, sheet must also be given.

By default, cells data are interpreted as being laid out side-by-side. names_row is a row index (starting from 0) to specify the row that contains the names of cells and parameters. Cells and parameter names must be contained in a single row. param_cols accepts a sequence (such as list or tuple) of column indexes (starting from 0) that indicate columns that contain cells arguments.

2-dimensional cells definitions

The optional names_col and param_rows parameters are used, when data for one cells spans more than one column. In such cases, the cells data is 2-dimensional, and there must be parameter row(s) across the columns that contain arguments of the parameters. A sequence of row indexes that indicate parameter rows is passed to param_rows. The names of those parameters must be contained in the same rows as parameter values (arguments), and names_col is to indicate the column position at which the parameter names are defined.

Horizontal arrangement

By default, cells data are interpreted as being placed side-by-side, regardless of whether one cells corresponds to a single column or multiple columns. transpose parameter is used to alter this orientation, and if it is set to True, cells values are interpreted as being placed one above the other. “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.

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).
  • 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.
  • param_order (optional) – a sequence to reorder the parameters. The elements of the sequence are the indexes of param_cols elements, and optionally the index of param_rows elements shifted by the length of param_cols.
has_params()[source]

Check if the parameter function is set.

formula

Property to get, set, delete formula.

set_formula(formula)[source]

Set if the parameter function.

to_frame(*args)[source]

Convert the space itself into a Pandas DataFrame object.

frame

Alias of to_frame().

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.