def __getattr__()

in tfx/types/artifact.py [0:0]


  def __getattr__(self, name: str) -> Any:
    """Custom __getattr__ to allow access to artifact properties."""
    if name == '_artifact_type':
      # Prevent infinite recursion when used with copy.deepcopy().
      raise AttributeError()
    if name not in self._artifact_type.properties:
      raise AttributeError('Artifact has no property %r.' % name)
    property_mlmd_type = self._artifact_type.properties[name]
    if property_mlmd_type == metadata_store_pb2.STRING:
      if name not in self._artifact.properties:
        # Avoid populating empty property protobuf with the [] operator.
        return ''
      return self._artifact.properties[name].string_value
    elif property_mlmd_type == metadata_store_pb2.INT:
      if name not in self._artifact.properties:
        # Avoid populating empty property protobuf with the [] operator.
        return 0
      return self._artifact.properties[name].int_value
    elif property_mlmd_type == metadata_store_pb2.DOUBLE:
      if name not in self._artifact.properties:
        # Avoid populating empty property protobuf with the [] operator.
        return 0.0
      return self._artifact.properties[name].double_value
    elif property_mlmd_type == metadata_store_pb2.STRUCT:
      if name not in self._artifact.properties:
        # Avoid populating empty property protobuf with the [] operator.
        return None
      if name in self._cached_modifiable_properties:
        return self._cached_modifiable_properties[name]
      value = _decode_struct_value(self._artifact.properties[name].struct_value)
      # We must cache the decoded lists or dictionaries returned here so that
      # if their recursive contents are modified, the Metadata proto message
      # can be updated to reflect this.
      if isinstance(value, (dict, list)):
        self._cached_modifiable_properties[name] = value
      return value
    elif property_mlmd_type == metadata_store_pb2.PROTO:
      if name not in self._artifact.properties:
        # Avoid populating empty property protobuf with the [] operator.
        return None
      if name in self._cached_modifiable_properties:
        return self._cached_modifiable_properties[name]
      value = proto_utils.unpack_proto_any(
          self._artifact.properties[name].proto_value)
      # We must cache the protobuf message here so that if its contents are
      # modified, the Metadata proto message can be updated to reflect this.
      self._cached_modifiable_properties[name] = value
      return value
    else:
      raise Exception('Unknown MLMD type %r for property %r.' %
                      (property_mlmd_type, name))