qhana_plugin_runner.plugin_utils.attributes module

Module containing helpers to work with entity attribute data and attribute metadata entities.

class qhana_plugin_runner.plugin_utils.attributes.AttributeMetadata(ID: str, attribute_type: str, title: str, description: str = '', multiple: bool = False, ordered: bool = False, separator: str = ';', ref_target: str | None = None, schema: str | None = None, extra: ~typing.Dict[str, ~typing.Any] = <factory>)

Bases: object

Dataclass for entity attribute metadata.

ID: str
attribute_type: str
description: str = ''
extra: Dict[str, Any]
static from_dict(metadata: Dict[str, str | bool]) AttributeMetadata

Create a AttributeMetadata instance from an entity dict.

multiple: bool = False
ordered: bool = False
ref_target: str | None = None
schema: str | None = None
separator: str = ';'
title: str
to_dict()

Get the attribute metadata as entity dict.

qhana_plugin_runner.plugin_utils.attributes.default_serialize(value: Any) str

Default value serializer.

None -> "" value: Union[bool, int, float, str] -> str(value) value: Any -> repr(value)

qhana_plugin_runner.plugin_utils.attributes.dict_deserializer(attributes: Iterable[str], attribute_metadata: Dict[str, AttributeMetadata], in_place: bool = True) Callable[[Dict[str, str]], Dict[str, Any]]

Create a dict de-serializer that parses the values of a dict from strings based on the attribute metadata.

The returned de-serializer parses the attributes of a single entity dict from their string values. It can be used in a list or generator comprehension or with map.

Parameters:
  • attributes (Iterable[str]) – the attribute names of the components of the dict (must be the same for all dicts!)

  • attribute_metadata (Dict[str, AttributeMetadata]) – the attribute metadata dict (see parse_attribute_metadata())

  • in_place (bool, optional) – if True the de-serialized values will replace the old values in the dict, if False a new dict is used. Defaults to True.

Returns:

the de-serializer function

Return type:

Callable[[Dict[str, str]], Dict[str, Any]]

qhana_plugin_runner.plugin_utils.attributes.dict_serializer(attributes: Iterable[str], attribute_metadata: Dict[str, AttributeMetadata], in_place: bool = True) Callable[[Dict[str, Any]], Dict[str, str]]

Create a dict serializer that serializes the values of a dict into strings based on the attribute metadata.

The returned serializer serializes the attributes of a single entity dict to a string. It can be used in a list or generator comprehension or with map.

Parameters:
  • attributes (Iterable[str]) – the attribute names of the components of the dict (must be the same for all dicts!)

  • attribute_metadata (Dict[str, AttributeMetadata]) – the attribute metadata dict (see parse_attribute_metadata())

  • in_place (bool, optional) – if True the serialized values will replace the old values in the dict, if False a new dict is used. Defaults to True.

Returns:

the serializer function

Return type:

Callable[[Dict[str, Any]], Dict[str, str]]

qhana_plugin_runner.plugin_utils.attributes.parse_attribute_metadata(metadata_entities: Iterable[Dict[str, Any]]) Dict[str, AttributeMetadata]

Parse a list or stream of entities into an attribute metadata dict mapping from attribute name to AttributeMetadata instance.

qhana_plugin_runner.plugin_utils.attributes.parse_bool(value: str | bool) bool

Parse a string value into a boolean.

Uses the sets CONSIDERED_TRUE and CONSIDERED_FALSE to determine the boolean value of the string.

Parameters:

value (Union[str, bool]) – the string to parse (is converted to lowercase and stripped of surrounding whitespace)

Raises:

ValueError – if the string cannot reliably be determined true or false

Returns:

the parsed result

Return type:

bool

qhana_plugin_runner.plugin_utils.attributes.parse_multi(value: str | ~typing.Sequence[str], deserialize: ~typing.Callable[[str], ~typing.Any], separator: str = ';', collection: ~typing.Type[~qhana_plugin_runner.plugin_utils.attributes.T] = <class 'list'>) T

Parse a multi value string into a collection of parsed values.

Parameters:
  • value (str) – the string to parse

  • deserialize (Callable[[str], Any]) – the deserialize function to parse single values

  • separator (str, optional) – the separator sequence that separates single values. Must not be part of the values! Defaults to “;”.

  • collection (Type[T], optional) – the collection class to use. Defaults to list.

Returns:

the collection of parsed values

Return type:

T

qhana_plugin_runner.plugin_utils.attributes.parse_optional_value(value: str, deserialize: Callable[[str], Any]) Any

Serialize optional values with the given serializer.

qhana_plugin_runner.plugin_utils.attributes.serialize_multi(value: Iterable[Any] | None, serialize: Callable[[Any], str], separator: str = ';') str

Serialize a list or set of values into a string.

Each value is serialized with the serializer and the values are joined with the separator.

Parameters:
  • value (Iterable[Any]|None) – the list or set of values to serialize

  • serialize (Callable[[Any], str]) – the serializer to use for single values

  • separator (str, optional) – the seperator to use between values. Must not be part of the serialized values! Defaults to “;”.

Returns:

the string of serialized items.

Return type:

str

qhana_plugin_runner.plugin_utils.attributes.tuple_deserializer(attributes: ~typing.Sequence[str], attribute_metadata: ~typing.Dict[str, ~qhana_plugin_runner.plugin_utils.attributes.AttributeMetadata], tuple_: ~typing.Callable[[~typing.Iterable], tuple] = <class 'tuple'>) Callable[[Tuple[str, ...]], Tuple[Any, ...]]

Create a tuple de-serializer that parses the values of a tuple from strings based on the attribute metadata.

The returned de-serializer parses the attributes of a single entity tuple from their string values. It can be used in a list or generator comprehension or with map.

Parameters:
  • attributes (Sequence[str]) – the attribute names of the components of the tuple (must be the same for all tuples!)

  • attribute_metadata (Dict[str, AttributeMetadata]) – the attribute metadata dict (see parse_attribute_metadata())

  • tuple (Type[tuple]) – the tuple type to use (should be a namedtuple._make function). Defaults to tuple()

Returns:

the de-serializer function

Return type:

Callable[[Tuple[str, …]], Tuple[Any, …]]

qhana_plugin_runner.plugin_utils.attributes.tuple_serializer(attributes: ~typing.Sequence[str], attribute_metadata: ~typing.Dict[str, ~qhana_plugin_runner.plugin_utils.attributes.AttributeMetadata], tuple_: ~typing.Callable[[~typing.Iterable], tuple] = <class 'tuple'>) Callable[[Tuple[Any, ...]], Tuple[str, ...]]

Create a tuple serializer that serializes the values of a tuple into strings based on the attribute metadata.

The returned serializer serializes the attributes of a single entity tuple to a string. It can be used in a list or generator comprehension or with map.

Parameters:
  • attributes (Sequence[str]) – the attribute names of the components of the tuple (must be the same for all tuples!)

  • attribute_metadata (Dict[str, AttributeMetadata]) – the attribute metadata dict (see parse_attribute_metadata())

  • tuple (Callable[[Iterable], tuple]) – the tuple type to use (should be a namedtuple._make function). Defaults to tuple()

Returns:

the serializer function

Return type:

Callable[[Tuple[Any, …]], Tuple[str, …]]