model.model.*

The model contains the core data structures of pyspread and is divided into the following layers.

Provides

class model.model.CellAttribute(selection: Selection, table: int, attr: AttrDict)[source]

Bases: NamedTuple

Single cell attribute

Create new instance of CellAttribute(selection, table, attr)

_asdict()

Return a new dict which maps field names to their values.

_field_defaults = {}
_fields = ('selection', 'table', 'attr')
classmethod _make(iterable)

Make a new CellAttribute object from a sequence or iterable

_replace(**kwds)

Return a new CellAttribute object replacing specified fields with new values

attr: AttrDict

Alias for field number 2

selection: Selection

Alias for field number 0

table: int

Alias for field number 1

class model.model.CellAttributes(*args, **kwargs)[source]

Bases: list

Stores cell formatting attributes in a list of CellAttribute instances

The class stores cell attributes as a list of layers. Each layer describes attributes for one selection in one table. Ultimately, a cell’s attributes are determined by going through all elements of an CellAttributes instance. A default AttrDict is updated with the one in the list element if it is relevant for the respective cell. Therefore, attributes are efficiently stored for large sets of cells.

The class provides attribute read access to single cells via __getitem__(). Otherwise it behaves similar to a list.

_attr_cache = {}
_len_table_cache()[source]

Returns the length of the table cache

Return type:

int

_table_cache = {}
_update_table_cache()[source]

Clears and updates the table cache to be in sync with self

append(cell_attribute)[source]

append that clears caches

Parameters:

cell_attribute (CellAttribute) – Cell attribute to be appended

for_table(table)[source]

Return cell attributes for a given table

Return type should be CellAttributes. The list return type is provided because PEP 563 is unavailable in Python 3.6.

Note that the table’s presence in the grid is not checked.

Parameters:

table (int) – Table for which cell attributes are returned

Return type:

list

get_merging_cell(key)[source]

Returns key of cell that merges the cell key

Retuns None if cell key not merged.

Parameters:

key (Tuple[int, int, int]) – Key of the cell that is merged

Return type:

Tuple[int, int, int]

class model.model.CodeArray(shape, settings)[source]

Bases: DataArray

CodeArray provides objects when accessing cells via __getitem__

Cell code can be accessed via function call

This class represents layer 3 of the model.

Parameters:
_eval_cell(key, code)[source]

Evaluates one cell and returns its result

Parameters:
  • key (Tuple[int, int, int]) – Key of cell to be evaled

  • code (str) – Code to be evaled

Return type:

Any

_get_updated_environment(env_dict=None)[source]

Returns globals environment with ‘magic’ variable

Parameters:

env_dict (dict) – Maps global variable name to value, None: {‘S’: self}

Return type:

dict

_make_nested_list(gen)[source]

Makes nested list from generator for creating numpy.array

Return type:

Union[Sequence, Sequence[Sequence], Sequence[Sequence[Sequence]]]

_sorted_keys(keys, startkey, reverse=False)[source]

Generator that yields sorted keys starting with startkey

Parameters:
Return type:

Iterable[Tuple[int, int, int]]

clear_globals()[source]

Clears all newly assigned globals

exec_then_eval(code, _globals=None, _locals=None)[source]

execs multiline code and returns eval of last code line

Parameters:
  • code (str) – Code to be executed / evaled

  • _globals (dict) – Globals dict for code execution and eval

  • _locals (dict) – Locals dict for code execution and eval

execute_macros()[source]

Executes all macros and returns result string and error string

Executes macros only when not in safe_mode

Return type:

Tuple[str, str]

findnextmatch(startkey, find_string, up=False, word=False, case=False, regexp=False, results=True)[source]

Returns tuple with position of the next match of find_string or None

Parameters:
  • startkey (Tuple[int, int, int]) – Start position of search

  • find_string (str) – String to be searched for

  • up (bool) – Search up instead of down if True

  • word (bool) – Search full words only if True

  • case (bool) – Search case sensitively if True

  • regexp (bool) – Regular expression search if True

  • results (bool) – Search includes result string if True (slower)

Return type:

Tuple[int, int, int]

frozen_cache = {}
get_globals()[source]

Returns globals dict

Return type:

dict

handler(signum, frame)[source]

Signal handler for timeout

Parameters:
  • signum (Any) – Ignored

  • frame (Any) – Ignored

pop(key)[source]

pop with cache support

Parameters:

key (Tuple[int, int, int]) – Cell key that shall be popped

reload_modules()[source]

Reloads modules that are available in cells

result_cache = {}
safe_mode = False
string_match(datastring, findstring, word, case, regexp)[source]

Returns position of findstring in datastring or None if not found

Parameters:
  • datastring (str) – String to be searched

  • findstring (str) – Search string

  • word (bool) – Search full words only if True

  • case (bool) – Search case sensitively if True

  • regexp (bool) – Regular expression search if True

Return type:

int

class model.model.DataArray(shape, settings)[source]

Bases: object

DataArray provides enhanced grid read/write access.

Enhancements comprise:
  • Slicing

  • Multi-dimensional operations such as insertion and deletion along one axis

This class represents layer 2 of the model.

Parameters:
_adjust_cell_attributes(insertion_point, no_to_insert, axis, tab=None, cell_attrs=None)[source]

Adjusts cell attributes on insertion/deletion

Parameters:
  • insertion_point (int) – Point on axis at which insertion takes place

  • no_to_insert (int) – Number of rows/cols/tabs to be inserted (>=0)

  • axis (int) – Row insertion if 0, column insertion if 1, must be in 0, 1

  • tab (int) – Table at which insertion takes place, None means all tables

  • cell_attrs (AttrDict) – If given replaces the existing cell attributes

_adjust_merge_area(attrs, insertion_point, no_to_insert, axis)[source]

Returns an updated merge area

Parameters:
  • attrs (AttrDict) – Cell attribute dictionary that shall be adjusted

  • insertion_point (int) – Point on axis at which insertion takes place

  • no_to_insert (int) – Number of rows/cols/tabs to be inserted (>=0)

  • axis (int) – Row insertion if 0, column insertion if 1, must be in 0, 1

Return type:

Tuple[int, int, int, int]

_adjust_rowcol(insertion_point, no_to_insert, axis, tab=None)[source]

Adjusts row and column sizes on insertion/deletion

Parameters:
  • insertion_point (int) – Point on axis at which insertion takes place

  • no_to_insert (int) – Number of rows or columns that are inserted

  • axis (int) – Row insertion if 0, column insertion if 1, must be in 0, 1

  • tab (int) – Table at which insertion takes place, None means all tables

_shift_rowcol(insertion_point, no_to_insert)[source]

Shifts row and column sizes when a table is inserted or deleted

Parameters:
  • insertion_point (int) – Table at which a new table is inserted

  • no_to_insert (int) – Number of tables that are inserted

cell_array_generator(key)[source]

Generator traversing cells specified in key

Yields cells’ contents.

Parameters:

key (Tuple[Union[int, slice], Union[int, slice], Union[int, slice]]) – Specifies the cell keys of the generator

Return type:

Iterable[str]

property cell_attributes: CellAttributes

cell_attributes interface to dict_grid

property col_widths: defaultdict

col_widths interface to dict_grid

property data: dict

Returns dict of data content.

  • Data is the central content interface for loading / saving data.

  • It shall be used for loading and saving from and to .pys and other files.

  • It shall be used for loading and saving macros.

  • However, it is not used for importing and exporting data because these operations are partial to the grid.

Content of returned dict

Parameters:
  • shape (Tuple[int, int, int]) – Grid shape

  • grid (Dict[Tuple[int, int, int], str]) – Cell content

  • attributes (CellAttribute) – Cell attributes

  • row_heights (defaultdict[Tuple[int, int], float]) – Row heights

  • col_widths (defaultdict[Tuple[int, int], float]) – Column widths

  • macros (str) – Macros

delete(deletion_point, no_to_delete, axis, tab=None)[source]

Deletes no_to_delete rows/cols/… starting with deletion_point

Parameters:
  • deletion_point (int) – Point on axis at which deletion takes place

  • no_to_delete (int) – Number of rows/cols/tabs to be deleted (>=0)

  • axis (int) – Row/Column/Table deletion if 0/1/2, must be in 0, 1, 2

  • tab (int) – Table at which insertion takes place, None means all tables

get_col_width(col, tab)[source]

Returns column width

Parameters:
  • col (int) – Column for which width is retrieved

  • tab (int) – Table for which for which column width is retrieved

Return type:

float

get_last_filled_cell(table=None)[source]

Returns key for the bottommost rightmost cell with content

Parameters:

table (int) – Limit search to this table

Return type:

Tuple[int, int, int]

get_row_height(row, tab)[source]

Returns row height

Parameters:
  • row (int) – Row for which height is retrieved

  • tab (int) – Table for which for which row height is retrieved

Return type:

float

insert(insertion_point, no_to_insert, axis, tab=None)[source]

Inserts no_to_insert rows/cols/tabs/… before insertion_point

Parameters:
  • insertion_point (int) – Point on axis at which insertion takes place

  • no_to_insert (int) – Number of rows/cols/tabs to be inserted (>=0)

  • axis (int) – Row/Column/Table insertion if 0/1/2 must be in 0, 1, 2

  • tab (int) – Table at which insertion takes place, None means all tables

keys()[source]

Returns keys in self.dict_grid

Return type:

List[Tuple[int, int, int]]

property macros: str

macros interface to dict_grid

pop(key)[source]

dict_grid pop wrapper

Parameters:

key (Tuple[int, int, int]) – Cell key

Return type:

Any

property row_heights: defaultdict

row_heights interface to dict_grid

set_col_width(col, tab, width)[source]

Sets column width

Parameters:
  • col (int) – Column for width setting

  • tab (int) – Table, in which column width is set

  • width (float) – Column width to be set

set_row_height(row, tab, height)[source]

Sets row height

Parameters:
  • row (int) – Row for height setting

  • tab (int) – Table, in which row height is set

  • height (float) – Row height to be set

property shape: Tuple[int, int, int]

Returns dict_grid shape

class model.model.DefaultCellAttributeDict[source]

Bases: AttrDict

Holds default values for all cell attributes

class model.model.DictGrid(shape)[source]

Bases: KeyValueStore

Core data class with all information that is stored in a .pys file.

Besides grid code access via standard dict operations, it provides the following attributes:

  • cell_attributes - Stores cell formatting attributes

  • macros - String of all macros

This class represents layer 1 of the model.

Parameters:

shape (Tuple[int, int, int]) – Shape of the grid

class model.model.KeyValueStore(default_value=None)[source]

Bases: dict

Key-Value store in memory. Currently a dict with default value None.

This class represents layer 0 of the model.

Parameters:

default_value – Value that is provided for missing keys