Skip to content

Commit

Permalink
ENH: Added preds and succs to Cells
Browse files Browse the repository at this point in the history
  • Loading branch information
fumitoh committed Nov 26, 2017
1 parent 490cc53 commit 262da24
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/source/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Classes
~modelx.core.model.Model
~modelx.core.space.SpaceContainer
~modelx.core.space.Space
~modelx.core.cells.Cells
~modelx.core.cells.Cells
~modelx.core.cells.CellNode
72 changes: 72 additions & 0 deletions modelx/core/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,57 @@ def to_frame(self):

return cells_to_dataframe(self)

# ----------------------------------------------------------------------
# Dependency

def predecessors(self, args, kwargs):
node = CellArgs(self, args, kwargs)
preds = self.model.cellgraph.predecessors(node)
for n in preds:
yield CellNode(n)

def successors(self, args, kwargs):
node = CellArgs(self, args, kwargs)
succs = self.model.cellgraph.successors(node)
for n in succs:
yield CellNode(n)


class CellNode:
"""A combination of a cells, its args and its value."""

def __init__(self, cellargs):
self._impl = cellargs

@property
def cells(self):
"""Return the Cells object"""
return self._impl.cells.interface

@property
def args(self):
"""Return a tuple of the cells' arguments."""
return self._impl.argvalues

@property
def has_value(self):
"""Return ``True`` if the cell has a value."""
return self._impl.cells.has_cell(self._impl.argvalues)

@property
def value(self):
"""Return the value of the cells."""
if self.has_value:
return self._impl.cells.get_value(self._impl.argvalues)
else:
raise ValueError('Value not found')

def __repr__(self):
if self.has_value:
return self._impl.__repr__() + '=' + str(self.value)
else:
return self._impl.__repr__()


class Cells(Interface, Container, Callable, Sized):
"""Data container with a formula to calculate its own values.
Expand Down Expand Up @@ -581,3 +632,24 @@ def set_formula(self, func):
def clear_formula(self):
"""Clear the formula."""
self._impl.clear_formula()

# ----------------------------------------------------------------------
# Dependency

def preds(self, *args, **kwargs):
"""Return an iterator over predecessors of a cell.
This method returns an iterator that yields CellNode objects,
each of which is a predecessor of (i.e. referenced in the formula
of) the cell specified by the given arguments.
"""
return self._impl.predecessors(args, kwargs)

def succs(self, *args, **kwargs):
"""Return an iterator over successors of a cell.
This method returns an iterator that yields CellNode objects,
each of which is a successor of (i.e. referencing in their formula)
the cell specified by the given arguments.
"""
return self._impl.successors(args, kwargs)

0 comments on commit 262da24

Please sign in to comment.