Exemple #1
0
    def import_module(self, module=None, recursive=False, **params):
        """Create a child space from an module.

        Args:
            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.
        """
        if module is None:
            if "module_" in params:
                warnings.warn(
                    "Parameter 'module_' is deprecated. Use 'module' instead.")
                module = params.pop("module_")
            else:
                raise ValueError("no module specified")

        if "bases" in params:
            params["bases"] = get_impls(params["bases"])

        space = (
            self._impl.model.currentspace
        ) = self._impl.new_space_from_module(
            module, recursive=recursive, **params
        )
        return get_interfaces(space)
Exemple #2
0
 def get_attr(self, name):
     if name in self.spaces:
         return self.spaces[name].interface
     elif name in self.global_refs:
         return get_interfaces(self.global_refs[name])
     else:
         raise AttributeError("Model '{0}' does not have '{1}'".format(
             self.name, name))
Exemple #3
0
    def repr_self(self, add_params=True):

        if add_params:
            args = [repr(arg) for arg in get_interfaces(self.argvalues)]
            param = ", ".join(args)
            return "%s[%s]" % (self.parent.name, param)
        else:
            return self.name
Exemple #4
0
    def new_cells_from_module(self, module):
        """Create a cells from a module.

        Alias to :py:meth:`import_funcs`.
        """
        # Outside formulas only
        newcells = self._impl.new_cells_from_module(module)
        return get_interfaces(newcells)
Exemple #5
0
def test_mro_simple(simplemodel):
    model = simplemodel
    C = model.new_space(name="C")
    A = model.new_space(name="A", bases=C)
    B = model.new_space(name="B", bases=C)
    D = model.new_space(name="D", bases=[A, B])

    assert get_interfaces(D._impl.mro) == [D, A, B, C]
Exemple #6
0
def test_mro_complicated(simplemodel):
    model = simplemodel
    o = model.new_space(name="o")
    f = model.new_space(name="f", bases=o)
    e = model.new_space(name="e", bases=o)
    d = model.new_space(name="d", bases=o)
    c = model.new_space(name="c", bases=[d, f])
    b = model.new_space(name="b", bases=[e, d])
    a = model.new_space(name="a", bases=[b, c])

    assert get_interfaces(a._impl.mro) == [a, b, e, c, d, f, o]
Exemple #7
0
    def new_space_from_module(self, module, recursive=False, **params):
        """Create a child space from an module.

        Alias to :py:meth:`import_module`.

        Args:
            module: a module object or name of the module object.
            recursive: Not yet implemented.
            **params: arguments to pass to :meth:`new_space`

        Returns:
            The new child space created from the module.
        """
        if "bases" in params:
            params["bases"] = get_impls(params["bases"])

        space = (
            self._impl.model.currentspace) = self._impl.new_space_from_module(
                module, recursive=recursive, **params)
        return get_interfaces(space)
Exemple #8
0
    def new_space_from_pandas(self,
                              obj,
                              space=None,
                              cells=None,
                              param=None,
                              space_params=None,
                              cells_params=None):
        """Create child spaces from Pandas DataFrame or Series.

        Create a space named ``space`` and optionally
        and cells in it from Pandas DataFrame or Series passed in ``obj``.
        If ``space`` is not given, the space is named ``SpaceN`` where
        ``N`` is automatically given by modelx.
        Parameter names are taken from ``obj`` indexes, unless ``param``
        is given to override index names.

        ``obj`` can have MultiIndex as its index.
        If the index(es) of ``obj``
        has/have name(s), the parameter name(s) of the cells is/are
        set to the name(s), but can be overwritten by ``param``
        parameter. If the index(es) of ``obj`` has/have no name(s),
        and ``param`` is not given, error is raised.

        Args:
            obj: DataFrame or Series.
            space: Space name.
            param: Sequence of strings to set parameter name(s).
                A single string can also be passed to set a single parameter
                name when ``frame`` has a single
                level index (i.e. not MultiIndex).
            space_params: Sequence of strings or integers to specify
                space parameters by name or index.
            cells_params: Sequence of strings or integers to specify
                cells parameters by name or index.

        See Also:
            :meth:`new_cells_from_pandas`: Create Cells from DataFrame or Series.
        """
        return get_interfaces(
            self._impl.new_space_from_pandas(obj, space, cells, param,
                                             space_params, cells_params))
Exemple #9
0
    def new_space_from_excel(
        self,
        book,
        range_,
        sheet=None,
        name=None,
        names_row=0,
        param_cols=(0,),
        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.

        Args:
            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.

        See Also:
            :meth:`UserSpace.new_cells_from_excel`: Create Cells from Excel file.
        """

        space = self._impl.new_space_from_excel(
            book,
            range_,
            sheet,
            name,
            names_row,
            param_cols,
            space_param_order,
            cells_param_order,
            transpose,
            names_col,
            param_rows,
        )

        return get_interfaces(space)
Exemple #10
0
 def import_funcs(self, module):
     """Create a cells from a module."""
     # Outside formulas only
     newcells = self._impl.new_cells_from_module(module)
     return get_interfaces(newcells)
Exemple #11
0
 def _direct_bases(self):
     """Directly inherited base classes"""
     return get_interfaces(self._impl.direct_bases)
Exemple #12
0
 def bases(self):
     """List of base classes."""
     return get_interfaces(self._impl.bases)
Exemple #13
0
 def evalrepr(self):
     """Evaluable repr"""
     args = [repr(arg) for arg in get_interfaces(self.argvalues)]
     param = ", ".join(args)
     return "%s(%s)" % (self.parent.evalrepr, param)
Exemple #14
0
 def _bind_args(self, args):
     self.boundargs = self.parent.formula.signature.bind(**args)
     self.argvalues = tuple(self.boundargs.arguments.values())
     self.argvalues_if = tuple(get_interfaces(self.argvalues))