def test_bad_serializer(self): orig = config.SERIALIZER config.SERIALIZER = "_not_dill_or_pickle_" try: with pytest.raises(ValueError): loads(b"") with pytest.raises(ValueError): dumps(None) finally: config.SERIALIZER = orig print(config)
def write(self, stream, prefix="", pretty=False): # type: (Union[IO, str], str, bool) -> None """Writes results in YAML format to a stream or file. Changing the parameter values from their defaults may result in the output becoming non-compatible with the YAML format. Parameters ---------- stream : file-like object or string A file-like object or a filename where results should be written to. prefix : string, optional A string to use as a prefix for each line that is written. (default: '') pretty : bool, optional Indicates whether or not certain recognized attributes should be formatted for more human-readable output. (default: False) Example ------- >>> import six >>> import pybnb >>> results = pybnb.SolverResults() >>> results.best_node = pybnb.Node() >>> results.best_node.objective = 123 >>> out = six.StringIO() >>> # the best_node is serialized >>> results.write(out) >>> del results >>> import yaml >>> results_dict = yaml.safe_load(out.getvalue()) >>> # de-serialize the best_node >>> best_node = pybnb.node.loads(results_dict['best_node']) >>> assert best_node.objective == 123 """ with as_stream(stream) as out: attrs = vars(self) names = sorted(list(attrs.keys())) first = ( "solution_status", "termination_condition", "objective", "bound", "absolute_gap", "relative_gap", "nodes", "wall_time", "best_node", ) for cnt, name in enumerate(first): if not hasattr(self, name): continue names.remove(name) val = getattr(self, name) if val is not None: if name in ("solution_status", "termination_condition"): if type(val) in (SolutionStatus, TerminationCondition): val = val.value elif pretty: if name == "wall_time": val = time_format(val, digits=2) elif name in ( "objective", "bound", "absolute_gap", "relative_gap", ): val = "%.7g" % (val) elif name == "best_node": assert isinstance(val, Node) if val.objective is not None: val = "Node(objective=%.7g)" % (val.objective) else: val = "Node(objective=None)" else: if name == "best_node": val = dumps(val) if not six.PY2: val = base64.encodebytes(val).decode("ascii") else: val = base64.encodestring(val).decode("ascii") val = "\n ".join(val.splitlines()) val = "!!binary |\n %s" % (val) else: val_ = "%r" % (val) if type(val) is float: if val_ == "inf": val_ = ".inf" elif val_ == "-inf": val_ = "-.inf" elif val_ == "nan": val_ = ".nan" val = val_ del val_ if pretty or (val is not None): out.write(prefix + "%s: %s\n" % (name, val)) else: assert val is None out.write(prefix + "%s: null\n" % (name)) for name in names: val = getattr(self, name) if pretty: out.write(prefix + "%s: %r\n" % (name, val)) else: if val is None: out.write(prefix + "%s: null\n" % (name)) else: val_ = "%r" % (val) if type(val) is float: if val_ == "inf": val_ = ".inf" elif val_ == "-inf": val_ = "-.inf" elif val_ == "nan": val_ = ".nan" val = val_ del val_ out.write(prefix + "%s: %s\n" % (name, val))