caikit.core.data_model.base

Base classes and functionality for all data structures.

Attributes

log

error

Classes

_DataBaseMetaClass

DataBase

Base class for all structures in the data model.

Module Contents

caikit.core.data_model.base.log[source]
caikit.core.data_model.base.error: Callable[Ellipsis, NoReturn]
class caikit.core.data_model.base._DataBaseMetaClass[source]

Bases: type

fields: Tuple
full_name: str
fields_enum_map: Dict
fields_enum_rev: Dict
_fields_oneofs_map: Dict
_fields_to_oneof: Dict
_fields_to_type: Dict
_fields_map: Tuple
_fields_message: Tuple
_fields_message_repeated: Tuple
_fields_enum: Tuple
_fields_enum_repeated: Tuple
_fields_primitive: Tuple
_fields_primitive_repeated: Tuple
_proto_class: ClassVar[Type[google.protobuf.message.Message]]

Meta class for all structures in the data model.

class_registry
_MISSING_ATTRIBUTE = 'missing attribute'
_FWD_DECL_FIELDS = '__fwd_decl_fields__'
_BACKEND_ATTR = '_backend'
_WHICH_ONEOF_ATTR = '_which_oneof'
_USER_DEFINED_DEFAULTS = '__user_defined_defaults__'
_PROTO_TYPE_ORDER
supports_file_operations = False
classmethod parse_proto_descriptor(cls)[source]

Encapsulate the logic for parsing the protobuf descriptor here. This allows the parsing to be done as a post-process after metaclass initialization

classmethod _make_property_getter(field, oneof_name=None)[source]

This helper creates an @property attribute getter for the given field

NOTE: This needs to live as a standalone function in order for the given

field name to be properly bound to the closure for the attrs

static _make_init(fields)[source]

This helper creates an __init__ function for a class which has the arguments for all the fields and just sets them as instance attributes.

classmethod _sorted_oneof_field_names(oneof: google.protobuf.descriptor.OneofDescriptor) List[str][source]

Helper to get the list of oneof fields while ensuring field names are sorted such that bool < int < float. This ensures that when iterating fields for which_oneof inference, lower-precedence types take precedence.

class caikit.core.data_model.base.DataBase[source]

Base class for all structures in the data model.

Notes:

All leaves in the hierarchy of derived classes should have a corresponding protobufs class defined in the interface definitions. If not, an exception will be thrown at runtime.

PROTO_CONVERSION_SPECIAL_TYPES
class OneofFieldVal[source]

Helper struct that backends can use to return information about values in oneofs along with which of the oneofs is currently valid

val: Any
which_oneof: str
__setattr__(name, val)[source]

Handle attribute setting for oneofs and named fields with delegation to backends as needed

classmethod get_proto_class() Type[google.protobuf.message.Message][source]
classmethod get_field_defaults() Type[google.protobuf.message.Message][source]

Get mapping of fields to default values. Mapping will not include fields without defaults

classmethod get_field_message_type(field_name: str) type | None[source]

Get the python type for the given field. This function relies on the metaclass to fill cls._fields_to_type. This is to avoid costly computation during runtime

Args:
field_name (str): Field name to check (AttributeError raised if name

is invalid)

Returns:
field_type: type

The data model class type for the given field

classmethod from_backend(backend)[source]
property backend: DataModelBackendBase | None
which_oneof(oneof_name: str) str | None[source]

Get the name of the oneof field set for the given oneof or None if no field is set

classmethod _infer_which_oneof(oneof_name: str, oneof_val: Any) str | None[source]

Check each candidate field within the oneof to see if it’s a type match

NOTE: In the case where fields within a oneof have the same type, the

first field whose type matches will be used!

_get_which_oneof_dict() Dict[str, str][source]
classmethod _get_type_for_field(field_name: str) type[source]

Helper class method to return the type hint for a particular field

classmethod _is_valid_type_for_field(field_name: str, val: Any) bool[source]

Check whether the given value is valid for the given field

classmethod from_binary_buffer(buf)[source]

Builds the data model object out of the binary string

Args:

buf: The binary buffer containing a serialized protobufs message

Returns:

A data model object instantiated from the protobufs message deserialized out of buf

classmethod from_proto(proto)[source]

Build a DataBase from protobufs.

Args:

proto: A protocol buffer to serialize from.

Returns:

protobufs: A DataBase object.

classmethod from_json(json_str, ignore_unknown_fields=False)[source]

Build a DataBase from a given JSON string. Use google’s protobufs.json_format for deserialization

Args:
json_str (str or dict): A stringified JSON specification/dict of the

data_model

ignore_unknown_fields (bool): If True, ignores unknown JSON fields

Returns:

caikit.core.data_model.DataBase: A DataBase object.

classmethod from_file(file_obj: io.IOBase)[source]
Abstractmethod:

Build a DataBase from a given file-like object.

Args:

file_obj IOBase: A file object that contains some representation of the dataobject

Returns:

caikit.core.data_model.DataBase: A DataBase object.

to_proto()[source]

Return a new protobufs populated with the information in this data structure.

to_binary_buffer()[source]

Returns a binary buffer with a serialized protobufs message of this data model

fill_proto(proto)[source]

Populate a protobufs with the values from this data model object.

Args:

proto: A protocol buffer to be populated.

Returns:

protobufs: The filled protobufs.

Notes:

The protobufs is filled in place, so the argument and the return value are the same at the end of this call.

to_dict() dict[source]

Convert to a dictionary representation.

to_kwargs() dict[source]

Convert to flat dictionary representation. (Like .to_dict, but not recursive) This keeps the attribute names of any fields backed by oneofs, instead of using the internal oneof field name

to_json(**kwargs) str[source]

Convert to a json representation.

abstract to_file(file_obj: io.IOBase) File | None[source]

Export a DataBaseObject into a file-like object file_obj. If the DataBase object has requirements around file name or file type it can return them via the optional “File” return object

Args:

file_obj IOBase: a file object to be filled

Returns:

file_descriptor: Optional[caikit.interfaces.common.data_mode.File]

__repr__()[source]

Human-friendly representation.

_field_to_dict_element(field)[source]

Convert field into a representation that can be placed into a dictionary. Recursively calls to_dict on other data model objects.

static get_class_for_proto(proto: google.protobuf.descriptor.Descriptor | google.protobuf.descriptor.FieldDescriptor | google.protobuf.descriptor.EnumDescriptor | google.protobuf.message.Message) Type[DataBase][source]

Look up the data model class corresponding to the given protobuf

If no data model is found, this raises an AttributeError

Args:
proto (Union[Descriptor, ProtoMessageType])

The proto name or descriptor to look up against

Returns:
dm_class (Type[DataBase]): The data model class corresponding to the

given protobuf

static get_class_for_name(class_name: str) Type[DataBase][source]

Look up the data model class corresponding to the given name

This lookup attempts to encode various naming conventions that might be used, but it can fail in multiple ways:

  1. No class with the given name is known

  2. Multiple classes with the same name, but different qualified parents are found

A ValueError will be raised if either of the above happens

Args:
class_name (str)

The name of the class either as a fully-qualified protobuf name or as the unqualified class name

Returns:
dm_class (Type[DataBase]): The data model class corresponding to the

given protobuf