jina.types.document.mixins.attribute module

class jina.types.document.mixins.attribute.GetSetAttributeMixin[source]

Bases: object

Provide helper functions for Document to allow advanced set and get attributes

classmethod attributes(include_proto_fields=True, include_proto_fields_camelcase=False, include_properties=False)[source]

Return all attributes supported by the Document, which can be accessed by doc.attribute

Parameters
  • include_proto_fields (bool) – if set, then include all protobuf fields

  • include_proto_fields_camelcase (bool) – if set, then include all protobuf fields in CamelCase

  • include_properties (bool) – if set, then include all properties defined for Document class

Return type

List[str]

Returns

a list of attributes in string.

get_attributes(*fields)[source]

Bulk fetch Document fields and return a list of the values of these fields

Note

Arguments will be extracted using dunder_get .. highlight:: python .. code-block:: python

d = Document({‘id’: ‘123’, ‘hello’: ‘world’, ‘tags’: {‘id’: ‘external_id’, ‘good’: ‘bye’}})

assert d.id == ‘123’ # true assert d.tags[‘hello’] == ‘world’ # true assert d.tags[‘good’] == ‘bye’ # true assert d.tags[‘id’] == ‘external_id’ # true

res = d.get_attrs_values(*[‘id’, ‘tags__hello’, ‘tags__good’, ‘tags__id’])

assert res == [‘123’, ‘world’, ‘bye’, ‘external_id’]

Parameters

fields (str) – the variable length values to extract from the document

Return type

Union[Any, List[Any]]

Returns

a list with the attributes of this document ordered as the args

update(source, fields=None)[source]

Updates fields specified in fields from the source to current Document.

Parameters
  • source (~T) – The Document we want to update from as source. The current Document is referred as destination.

  • fields (Optional[List[str]]) – a list of field names that we want to update, if not specified, use all present fields in source.

Note

*. if fields are empty, then all present fields in source will be merged into current document. * tags will be updated like a python dict. *. the current Document will be modified in place, source will be unchanged. *. if current document has more fields than source, these extra fields wll be preserved.

Return type

None