hermes.model.merge.container

Classes

_ld_merge_container

Abstract base class for ld_merge_dict and ld_merge_list,

ld_merge_list

ld_list wrapper to ensure the 'merge_container'-property does not get lost, while merging.

ld_merge_dict

ld_dict wrapper providing methods to merge an object of this class with an ld_dict object.

Module Contents

class hermes.model.merge.container._ld_merge_container

Abstract base class for ld_merge_dict and ld_merge_list, providing the merge containers with an override of ld_container._to_native_python(). See also ld_dict, ld_list and ld_container.

_to_native_python(full_iri: str, ld_value: hermes.model.types.ld_container.EXPANDED_JSON_LD_VALUE | dict[str, hermes.model.types.ld_container.EXPANDED_JSON_LD_VALUE] | list[str] | str) ld_merge_dict | ld_merge_list | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE

Returns a native python version of ld_value pretending the value is in self and full_iri its key.

Parameters:
  • full_iri (str) – The expanded iri of the key of ld_value / self (later if self is not a dictionary).

  • ld_value (EXPANDED_JSON_LD_VALUE | dict[str, EXPANDED_JSON_LD_VALUE] | list[str] | str) – The value thats native python value is requested. ld_value has to be valid expanded JSON-LD if it was embeded in self._data.

Returns:

The native python value of ld_value.

Return type:

ld_merge_dict | ld_merge_list | BASIC_TYPE | TIME_TYPE

class hermes.model.merge.container.ld_merge_list(data: list[str] | list[dict[str, hermes.model.types.ld_container.EXPANDED_JSON_LD_VALUE]], prov_doc: hermes.model.provenance.ld_prov.ld_prov_list = None, prov_objects: list[hermes.model.types.ld_dict] = 3 * [None], *, parent: hermes.model.types.ld_container | None = None, key: str | None = None, index: int | None = None, context: list[str | hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT] | None = None, strategies: dict[str | None, dict[str | None, hermes.model.merge.action.MergeAction]] = {})

Bases: _ld_merge_container, hermes.model.types.ld_list

ld_list wrapper to ensure the ‘merge_container’-property does not get lost, while merging. See also ld_list and ld_merge_container.

strategies

The strategies used inside the child ld_merge_dicts.

Type:

dict[str | None, dict[str | None, MergeAction]]

class hermes.model.merge.container.ld_merge_dict(data: list[dict[str, hermes.model.types.ld_container.EXPANDED_JSON_LD_VALUE]], prov_doc: hermes.model.provenance.ld_prov.ld_prov_list = None, prov_objects: list[hermes.model.types.ld_dict] = 3 * [None], *, parent: hermes.model.types.ld_dict | hermes.model.types.ld_list | None = None, key: str | None = None, index: int | None = None, context: list[str | hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT] | None = None, strategies: dict[str | None, dict[str | None, hermes.model.merge.action.MergeAction]] = {})

Bases: _ld_merge_container, hermes.model.types.ld_dict

ld_dict wrapper providing methods to merge an object of this class with an ld_dict object. See also ld_dict and ld_merge_container.

strategies

The strategies for merging different types of values in the ld_dicts.

Type:

dict[str | None, dict[str | None, MergeAction]]

update_context(other_context: list[str | hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT] | None) None

Updates self s context with other_context. JSON-LD processing prioritizes the context values in order (first least important, last most important).

Parameters:

other_context (list[str | JSON_LD_CONTEXT_DICT] | None) – The context object that is added to self s context.

Return type:

None

update(other: hermes.model.types.ld_dict) None

Updates/ Merges self with the given ld_dict other. Note that this overwrites ld_dict.update(), and may cause unexpected behavior if not used carefully.

Parameters:

other (ld_dict) – The ld_container that is merged into self.

Return type:

None

add_strategy(strategy: dict[str | None, dict[str | None, hermes.model.merge.action.MergeAction]]) None

Adds strategy to the self.strategies.

Parameters:

strategy (dict[str | None, dict[str | None, MergeAction]]) – The object describing how which object types are supposed to be merged.

Return type:

None

__setitem__(key: str, value: hermes.model.types.ld_container.JSON_LD_VALUE | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict | hermes.model.types.ld_list) None

Creates the new entry for self[key] using self.strategies on the values in self[key] and value. Note that this overwrites ld_dict.__setitem__() and may cause unexpected behavior if not used carefully.

Parameters:
  • key (str) – The key at which the value is updated/ merged at in self.

  • value (JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The value that is merged into self[key].

match(key: str, value: hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict | hermes.model.types.ld_list, match: Callable[[Any, Any], bool]) hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | ld_merge_dict | ld_merge_list

Returns the first item in self[key] for which match(item, value) returns True. If no such item is found None is returned instead.

Parameters:
  • key (str) – The key to the items in self from which a match for value is searched.

  • value (Union[JSON_LD_VALUE, BASIC_TYPE, TIME_TYPE, ld_dict, ld_list]) – The value a match is searched for in self[key].

  • match (Callable[[Any, Any], bool]) – The method defining if two objects are a match.

Returns:

The item in self[key] that is a match for``value`` if one exists otherwise None.

Return type:

BASIC_TYPE | TIME_TYPE | ld_merge_dict | ld_merge_list

_merge_item(key: str, value: hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict | hermes.model.types.ld_list) hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | ld_merge_dict | ld_merge_list

Applies the most suitable merge strategy to merge self[key] and value and then returns the result.

Parameters:
  • key (str) – The key to the entry in self that is to be merged with value.

  • value (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The value that is to be merged with self[key].

Returns:

The result of the merge from self[key] with value.

Return type:

BASIC_TYPE | TIME_TYPE | ld_merge_dict | ld_merge_list

Raises:

MergeError – If there is no strategy for this key.

Adds an entry for rel to self containing which key and value is affected.

Parameters:
  • rel (str) – The “type” of the special entry (used as the key).

  • key (str) – The key of the affected key, value pair in self.

  • value (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The value of the affected key, value pair in self.

Return type:

None

reject(key: str, value: hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict | hermes.model.types.ld_list) None

Adds an entry to self containing containing information that the key, value pair key, value has been rejected in the merge. For further information see ld_merge_dict._add_related().

Parameters:
  • key (str) – The key of the rejected key, value pair in self.

  • value (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The value of the rejected key, value pair in self.

Return type:

None

replace(key: str, value: hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict | hermes.model.types.ld_list) None

Adds an entry to self containing containing information that the key, value pair key, value was replaced in the merge. For further information see ld_merge_dict._add_related().

Parameters:
  • key (str) – The key of the old key, value pair in self.

  • value (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The value of the old key, value pair in self.

Return type:

None