def replace_endpoints(self, d): children = OrderedDict() for k, v in d.items(): assert isinstance(k, str_), "Expected string, got %s" % (k, ) if k == "typeid": assert v == self.typeid, \ "Dict has typeid %s but Class has %s" % (v, self.typeid) else: try: object.__getattribute__(self, k) except AttributeError: children[k] = deserialize_object(v, self.child_type_check) else: raise AttributeError( "Setting child %r would shadow an attribute" % (k, )) self.endpoints = list(children) for k, v in children.items(): self.set_endpoint_data(k, v, notify=False) if self.process: self.process.report_changes( [self.process_path, serialize_object(self)])
def replace_endpoints(self, d): children = OrderedDict() for k, v in d.items(): assert isinstance(k, str_), "Expected string, got %s" % (k,) if k == "typeid": assert v == self.typeid, \ "Dict has typeid %s but Class has %s" % (v, self.typeid) else: try: object.__getattribute__(self, k) except AttributeError: children[k] = deserialize_object(v, self.child_type_check) else: raise AttributeError( "Setting child %r would shadow an attribute" % (k,)) self.endpoints = list(children) for k, v in children.items(): self.set_endpoint_data(k, v, notify=False) if self.process: self.process.report_changes( [self.process_path, serialize_object(self)])
def set_endpoint_data(self, name, value, notify=True): # set parent first so that we don't put something in the tree that # doesn't know how to get the path to the top of the tree if hasattr(value, "set_parent"): value.set_parent(self, name) super(Monitorable, self).set_endpoint_data(name, value) if notify: self.report_changes([[name], serialize_object(value)])
def serialize_hook(o): o = serialize_object(o) if isinstance(o, (np.number, np.bool_)): return o.tolist() elif isinstance(o, np.ndarray): assert len(o.shape) == 1, "Expected 1d array, got {}".format(o.shape) return o.tolist() else: return o
def serialize_hook(o): o = serialize_object(o) if isinstance(o, np.number): return o.tolist() elif isinstance(o, np.ndarray): assert len(o.shape) == 1, "Expected 1d array, got {}".format(o.shape) return o.tolist() else: return o
def set_endpoint_data(self, name, value, notify=True): # set parent first so that we don't put something in the tree that # doesn't know how to get the path to the top of the tree process_path = self.process_path + [name] if hasattr(value, "set_process_path") and self.process: value.set_process_path(self.process, process_path) super(Monitorable, self).set_endpoint_data(name, value) if notify and self.process: self.process.report_changes( [process_path, serialize_object(value)])
def apply_changes(self, *changes): serialized_changes = [] for path, value in changes: ob = self for node in path[:-1]: ob = ob[node] attr = path[-1] setter = getattr(ob, "set_%s" % attr) setter(value, notify=False) serialized = serialize_object(ob[attr]) serialized_changes.append([path, serialized]) self.report_changes(*serialized_changes)
def apply_changes(self, *changes): serialized_changes = [] for path, value in changes: ob = self for node in path[:-1]: ob = ob[node] attr = path[-1] setter = getattr(ob, "set_%s" % attr) setter(value, notify=False) serialized = serialize_object(ob[attr]) serialized_changes.append([self.process_path + path, serialized]) if self.process: self.process.report_changes(*serialized_changes)
def set_endpoint(self, type_, name, value, notify=True): if isinstance(type_, list): assert len(type_) == 1, \ "Can't deal with multi-type list %s" % (type_,) assert isinstance(value, list), \ "Expected list, got %s" % (value,) value = [self._cast(v, type_[0]) for v in value] elif isinstance(type_, dict): assert len(type_) == 1, \ "Can't deal with multi-type dict %s" % (type_,) ktype, vtype = list(type_.items())[0] assert isinstance(value, dict), \ "Expected dict, got %s" % (value,) for k, v in value.items(): assert k == self._cast(k, ktype), \ "Changing of key types not supported" value[k] = self._cast(v, vtype) elif type_ is not NO_VALIDATE: value = self._cast(value, type_) setattr(self, name, value) if hasattr(value, "set_parent"): value.set_parent(self, name) self.on_changed([[name], serialize_object(value)], notify)
def to_dict(self): return serialize_object(self.data)
def test_exception_serialize(self): s = json_encode(serialize_object({"message": ValueError("Bad result")})) assert s == '{"message": "ValueError: Bad result"}'
def set_parameters(self, parameters): if parameters is not None: parameters = OrderedDict( (deserialize_object(k, str_), serialize_object(v)) for k, v in parameters.items()) self.set_endpoint_data("parameters", parameters)
def set_value(self, value): self.set_endpoint_data("value", serialize_object(value))