Esempio n. 1
0
    def serialize_model_as_file(self, mdl):
        # step 1 : prints the model in whatever exchange format
        printer = self._new_printer(ctx=mdl.context)

        if self._exchange_format.is_binary:
            filemode = "w+b"
        else:
            filemode = "w+"

        lp_output = tempfile.NamedTemporaryFile(mode=filemode, delete=False)

        printer.printModel(mdl, lp_output)

        # lp name to docplex var
        self._lpname_to_var_map = printer.get_name_to_var_map(mdl)

        # DEBUG: dump request file
        if self.debug_dump:
            dump_path = make_path(error_handler=mdl.error_handler,
                                  basename=mdl.name,
                                  output_dir=self.debug_dump_dir,
                                  extension=printer.extension(),
                                  name_transformer="docloud_%s")
            print("DEBUG DUMP in " + dump_path)
            with open(dump_path, filemode) as out_file:
                lp_output.seek(0)
                out_file.write(lp_output)

        lp_output.close()
        return lp_output.name
Esempio n. 2
0
    def serialize_model(self, mdl):
        # step 1 : prints the model in whatever exchange format
        printer = self._new_printer(ctx=mdl.context)

        if self._exchange_format.is_binary:
            filemode = "wb"
            oss = BytesIO()
        else:
            filemode = "w"
            oss = StringIO()

        printer.printModel(mdl, oss)

        # lp name to docplex var
        self._lpname_to_var_map = printer.get_name_to_var_map(mdl)

        # DEBUG: dump request file
        if self.debug_dump:
            dump_path = make_path(error_handler=mdl.error_handler,
                                  basename=mdl.name,
                                  output_dir=self.debug_dump_dir,
                                  extension=printer.extension(),
                                  name_transformer="docloud_%s")
            print("DEBUG DUMP in " + dump_path)
            with open(dump_path, filemode) as out_file:
                out_file.write(oss.getvalue())

        if self._exchange_format.is_binary:
            model_data = oss.getvalue()
        else:
            model_data = oss.getvalue().encode('utf-8')
        return model_data
Esempio n. 3
0
 def _dump_if_required(self, data, mdl, basename, extension, is_binary=False, forced=False):
     # INTERNAL
     if self.debug_dump or forced:
         relax_path = make_path(error_handler=mdl.error_handler,
                                basename=basename,
                                output_dir=self.debug_dump_dir,
                                extension=extension,
                                name_transformer=None)
         if isinstance(data, bytes) or isinstance(data, bytearray):
             is_binary = True  # For binary when data is binary
         fmode = "wb" if is_binary else "w"
         with open(relax_path, fmode) as out_file:
             out_file.write(data)