示例#1
0
    def add_attributes(self, attrs):
        """
        Adds potentially multiple attributes to this dataset version.

        Parameters
        ----------
        attributes : dict of str to {None, bool, float, int, str, list, dict}
            Attributes.

        """
        # validate all keys first
        for key in six.viewkeys(attrs):
            _utils.validate_flat_key(key)

        # build KeyValues
        attribute_keyvals = []
        for key, value in six.viewitems(attrs):
            attribute_keyvals.append(
                _CommonCommonService.KeyValue(
                    key=key,
                    value=_utils.python_to_val_proto(value,
                                                     allow_collection=True)))

        Message = _DatasetVersionService.AddDatasetVersionAttributes
        msg = Message(id=self.id, attributes=attribute_keyvals)
        endpoint = "/api/v1/modeldb/dataset-version/addDatasetVersionAttributes"
        self._update(msg, Message.Response, endpoint, "POST")
示例#2
0
    def del_attribute(self, key):
        """
        Deletes the attribute with name `key` from this Model Version

        Parameters
        ----------
        key : str
            Name of the attribute.

        """
        _utils.validate_flat_key(key)

        self._fetch_with_no_cache()
        attributes = list(
            filter(lambda attribute: attribute.key == key,
                   self._msg.attributes))
        if attributes:
            self._msg.attributes.remove(attributes[0])
            self._update(self._msg, method="PUT")
示例#3
0
    def del_attribute(self, key):
        """
        Deletes the attribute with name `key` from this dataset.

        This method will not raise an error if the attribute does not exist.

        Parameters
        ----------
        key : str
            Name of the attribute.

        """
        _utils.validate_flat_key(key)

        # build KeyValues
        Message = _DatasetService.DeleteDatasetAttributes
        msg = Message(id=self.id, attribute_keys=[key])
        endpoint = "/api/v1/modeldb/dataset/deleteDatasetAttributes"
        self._update(msg, Message.Response, endpoint, "DELETE")
示例#4
0
    def get_attribute(self, key):
        """
        Gets the attribute with name `key` from this dataset.

        Parameters
        ----------
        key : str
            Name of the attribute.

        Returns
        -------
        one of {None, bool, float, int, str}
            Value of the attribute.

        """
        _utils.validate_flat_key(key)
        attributes = self.get_attributes()

        try:
            return attributes[key]
        except KeyError:
            six.raise_from(KeyError("no attribute found with key {}".format(key)), None)
示例#5
0
    def add_attributes(self, attrs, overwrite=False):
        """
        Adds potentially multiple attributes to this Model Version.

        Parameters
        ----------
        attrs : dict of str to {None, bool, float, int, str, list, dict}
            Attributes.
        overwrite : bool, default False
            Whether to allow overwriting an existing attribute with key `key`.

        """
        # validate all keys first
        for key in six.viewkeys(attrs):
            _utils.validate_flat_key(key)
        # convert data_types to dicts
        for key, value in attrs.items():
            if isinstance(value, data_types._VertaDataType):
                attrs[key] = value._as_dict()

        # build KeyValues
        attribute_keyvals = []
        existing_attrs = self.get_attributes()
        for key, value in six.viewitems(attrs):
            if key in existing_attrs and not overwrite:
                warnings.warn("skipping attribute {} which already exists;"
                              " set `overwrite=True` to overwrite".format(key))
                continue

            attribute_keyvals.append(
                _CommonCommonService.KeyValue(
                    key=key,
                    value=_utils.python_to_val_proto(value,
                                                     allow_collection=True),
                ))

        self._update(self.ModelVersionMessage(attributes=attribute_keyvals))