Ejemplo n.º 1
0
    def zip(self,
            model_path,
            backup=True,
            log_input=False,
            compression=zipfile.ZIP_DEFLATED,
            compresslevel=None):
        """Archive model to a zip file.

        This method performs the :py:func:`~modelx.zip_model`
        on self. See :py:func:`~modelx.zip_model` section for the details.

        .. versionchanged:: 0.9.0
            ``compression`` and ``compresslevel`` parameters are added.

        .. versionadded:: 0.8.0

        Args:
            model_path(str): Folder(directory) path where the model is saved.
            backup(bool, optional): Whether to backup an existing file with
                the same name if it already exists. Defaults to ``True``.
            log_input(bool, optional): If ``True``, input values in Cells are
                output to *_input_log.txt* under ``model_path``. Defaults
                to ``False``.
            compression(optional): Identifier of the ZIP compression method
                to use. This method uses `zipfile.ZipFile`_ class internally
                and ``compression`` and ``compresslevel`` arguments are
                passed to `zipfile.ZipFile`_ constructor.
                See `zipfile.ZipFile`_ manual page for available identifiers.
                Defaults to `zipfile.ZIP_DEFLATED`_.
            compresslevel(optional):
                Integer identifier to indicate the compression level to use.
                If not specified, the default compression level is used.
                See `zipfile.ZipFile`_ explanation on the Python Standard
                Library site for available integer identifiers for
                each compression method.
                For Python 3.6, this parameter is ignored.

        .. _zipfile.ZipFile:
           https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile

        .. _zipfile.ZIP_DEFLATED:
           https://docs.python.org/3/library/zipfile.html#zipfile.ZIP_DEFLATED

        """
        from modelx.serialize import write_model
        write_model(self._impl.system,
                    self,
                    model_path,
                    is_zip=True,
                    backup=backup,
                    log_input=log_input,
                    compression=compression,
                    compresslevel=compresslevel)
Ejemplo n.º 2
0
    def write(self, model_path):
        """Write model to files.

        This method performs the :py:func:`~modelx.write_model`
        on self. See :py:func:`~modelx.write_model` section for the details.

        .. versionadded:: 0.0.22

        Args:
            model_path(str): Folder(directory) path where the model is saved.
        """
        from modelx.serialize import write_model
        write_model(self._impl.system, self, model_path)
Ejemplo n.º 3
0
def write_model(model, model_path, backup=True, log_input=False, version=None):
    """Write model to files.

    Write ``model`` to text files in a folder(directory) tree at ``model_path``.

    Model attributes, such as its name and refs, are output in the file
    named *_model.py*, directly under `model_path`.
    For each space in the model, a text file is created with the same name
    as the space with ".py" extension. The tree structure of the spaces
    is represented by the tree of folders, i.e. child spaces
    of a space is stored in a folder named the space.

    Generated text files are Python pseudo-scripts, i.e. they are
    syntactically correct but semantically not-correct Python scripts,
    that can only be interpreted through :py:func:`~read_model` function.

    Dynamic spaces and cells values are not stored.

    For spaces and cells created
    by :py:meth:`~modelx.core.space.UserSpace.new_space_from_excel` and
    :py:meth:`~modelx.core.space.UserSpace.new_cells_from_excel`,
    the source Excel files are copied into the same directory where
    the text files for the spaces the methods are associated with are located.
    Then when the model is read by :py:func:`~read_model` function,
    the methods are invoked to create the spaces or cells.

    Method :py:meth:`~modelx.core.model.Model.write` performs the same operation.

    .. versionchanged:: 0.8.0 ``log_input`` parameter is added.

    .. versionchanged:: 0.1.0 ``version`` parameter is added.

    .. versionadded:: 0.0.22

    Warning:
        The order of members of each type (Space, Cells, Ref)
        is not preserved by :func:`write_model` and :func:`read_model`.

    Args:
        model: Model object to write.
        model_path(str): Folder path where the model will be output.
        backup(bool, optional): Whether to backup the directory/folder
            if it already exists. Defaults to ``True``.
        log_input(bool, optional): If ``True``, input values in Cells are
            output to *_input_log.txt* under ``model_path``. Defaults
            to ``False``.
        version(int, optional): Format version to write model.
            Defaults to the most recent version.

    """
    return _serialize.write_model(_system,
                                  model,
                                  model_path,
                                  is_zip=False,
                                  backup=backup,
                                  log_input=log_input,
                                  version=version)
Ejemplo n.º 4
0
def zip_model(model, model_path, backup=True, log_input=False,
              compression=zipfile.ZIP_DEFLATED, compresslevel=None,
              version=None):
    """Archive model to a zip file

    Write ``model`` to a single zip file. The contents are the
    same as the directory tree output by the :func:`write_model` function.

    .. versionchanged:: 0.9.0
        ``compression`` and ``compresslevel`` parameters are added.

    .. versionadded:: 0.8.0

    Args:
        model: Model object to archive.
        model_path(str): Path to the zip file.
        backup(bool, optional): Whether to backup an existing file with
            the same name if it already exists. Defaults to ``True``.
        log_input(bool, optional): If ``True``, input values in Cells are
            output to *_input_log.txt* under ``model_path``. Defaults
            to ``False``.
        compression(optional): Identifier of the ZIP compression method
            to use. This method uses `zipfile.ZipFile`_ class internally
            and ``compression`` and ``compresslevel`` arguments are
            passed to `zipfile.ZipFile`_ constructor.
            See `zipfile.ZipFile`_ manual page for available identifiers.
            Defaults to `zipfile.ZIP_DEFLATED`_.
        compresslevel(optional):
            Integer identifier to indicate the compression level to use.
            If not specified, the default compression level is used.
            See `zipfile.ZipFile`_ explanation on the Python Standard
            Library site for available integer identifiers for
            each compression method.
            For Python 3.6, this parameter is ignored.

        version(int, optional): Format version to write model.
            Defaults to the most recent version.
            This parameter should be left unspecified in normal cases.

    .. _zipfile.ZipFile:
       https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile

    .. _zipfile.ZIP_DEFLATED:
       https://docs.python.org/3/library/zipfile.html#zipfile.ZIP_DEFLATED

    See Also:
        :func:`write_model`
    """
    return _serialize.write_model(
        _system, model, model_path, is_zip=True,
        backup=backup, log_input=log_input,
        compression=compression, compresslevel=compresslevel,
        version=version)
Ejemplo n.º 5
0
    def write(self, model_path, backup=True, log_input=False):
        """Write model to files.

        This method performs the :py:func:`~modelx.write_model`
        on self. See :py:func:`~modelx.write_model` section for the details.

        .. versionchanged:: 0.8.0
        .. versionadded:: 0.0.22

        Args:
            model_path(str): Folder(directory) path where the model is saved.
            backup(bool, optional): Whether to backup an existing file with
                the same name if it already exists. Defaults to ``True``.
            log_input(bool, optional): If ``True``, input values in Cells are
                output to *_input_log.txt* under ``model_path``. Defaults
                to ``False``.
        """
        from modelx.serialize import write_model
        write_model(self._impl.system,
                    self,
                    model_path,
                    is_zip=False,
                    backup=backup,
                    log_input=log_input)