hermes.model.types.ld_list

Classes

ld_list

An JSON-LD container resembling a list ("@set", "@list" or "@graph").

Module Contents

class hermes.model.types.ld_list.ld_list(data: hermes.model.types.ld_container.EXPANDED_JSON_LD_VALUE, *, parent: hermes.model.types.ld_dict.ld_dict | 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)

Bases: hermes.model.types.ld_container.ld_container

An JSON-LD container resembling a list (“@set”, “@list” or “@graph”). See also ld_container.

container_type

The type of JSON-LD container the list is representing. (“@set”, “@list”, “graph”)

Type:

str

item_list

The list of items (in expanded JSON-LD form) that are contained in this ld_list.

Type:

EXPANDED_JSON_LD_VALUE

__getitem__(index: int | slice) → hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict.ld_dict | ld_list | list[hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict.ld_dict | ld_list]

Get the item(s) at position index in their native python version.

Parameters:

index (int | slice) – The positon(s) from which the item(s) is/ are taken.

Returns:

The item(s) at index in the native python version.

Return type:

BASIC_TYPE | TIME_TYPE | ld_dict | ld_list | list[BASIC_TYPE | TIME_TYPE | ld_dict | ld_list]

__setitem__(index: int | slice, 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.ld_dict | ld_list) → None

Set the item(s) at position index to the given value(s).

All given values are expanded. If any are assimilated by self all items that would be added by this are added.

Parameters:
  • index (int | slice) – The positon(s) at which the item(s) is/ are set.

  • value (JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The new value(s).

Return type:

None

Raises:

TypeError – If a slice is not assigned an iterable.

__delitem__(index: int | slice) → None

Delete the item(s) at position index.

Note that if a deleted object is represented by an ld_container druing this process it will still exist and not be modified afterwards.

Parameters:

index (int | slice) – The positon(s) at which the item(s) is/ are deleted.

Return type:

None

__len__() → int

Returns the number of items in this ld_list.

Returns:

The length of self.

Return type:

int

__iter__() → collections.abc.Generator[hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_dict.ld_dict | ld_list, None, None]

Returns an iterator over the native python version of the values contained in self.

Returns:

The Iterator over self’s values.

Return type:

Generator[BASIC_TYPE | TIME_TYPE | ld_dict | ld_list, None, None]

__contains__(value: hermes.model.types.ld_container.JSON_LD_VALUE) → bool

Returns whether or not value is contained in self.

Note that it is not directly checked if value is in self.item_list: First value is expanded then it is checked if value is in self.item_list. If however value is assimilated by self it is checked if all values are contained in self.item_list.

Also note that the checks whether the expanded value is in self.item_list is based on ld_list.__eq__. That means that this value is ‘contained’ in self.item_list if any object in self.item_list has the same @id like it or it xor the object in the item_list has an id an all other values are the same.

Parameters:

value (JSON_LD_VALUE) – The object being checked whether or not it is in self.

Returns:

Whether or not value is being considered to be contained in self.

Return type:

bool

__eq__(other: ld_list | list[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.ld_dict | ld_list] | dict[str, 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.ld_dict | ld_list]) → bool

Returns wheter or not self is considered to be equal to other.

If other is not an ld_list, it is converted first. For each index it is checked if the ids of the items at index in self and other match if both have one, if only one has or neither have an id all other values are compared.

Note that due to those circumstances equality is not transitve meaning if a == b and b == c it is not guaranteed that a == c.

If self or other is considered unordered the comparison is more difficult. All items in self are compared with all items in other. On the resulting graph given by the realtion == the Hopcroft-Karp algoritm is used to determine if there exists a bijection reordering self so that the ordered comparison of self with other returns true.

Parameters:

other (ld_list | list[JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list]) – The list/ container/ ld_list self is compared to.

Returns:

Whether or not self and other are considered equal.

If other is of the wrong type return the NotImplemented singleton instead.

Return type:

bool

classmethod _bfs_step(verticies1: set[collections.abc.Hashable], edges: dict[collections.abc.Hashable, tuple[collections.abc.Hashable]], matches: dict[collections.abc.Hashable, collections.abc.Hashable], distances: dict[collections.abc.Hashable, int | float]) → bool

Completes the BFS step of Hopcroft-Karp. I.e.:

Finds the shortest path from all unmatched verticies in verticies1 to any unmatched vertex in any value in edges where the connecting paths are alternating between matches and its complement.

It also marks each vertex in verticies1 with how few verticies from verticies1 have to be passed to reach the vertex from an unmatched one in verticies1. This is stored in distances.

Parameters:
  • verticies1 (set[Hashable]) – The set of verticies in the left partition of the bipartite graph.

  • edges (dict[Hashable, tuple[Hashable]]) – The edges in the bipartite graph. (As the edges are bidirectional they are expected to be given in this format: Dictionary with keys being the vertices in the left partition and values being tuples of verticies in the right partition.)

  • matches (dict[Hashable, Hashable]) – The current matching of verticies in the left partition with the ones in the right partition.

  • distances (dict[Hashable, Union[int, float]]) – The reference to the dictionary mapping verticies of the left partition to the minimal number of verticies in the left partition that will be passed on a path from an unmatched vertex of the left partition to the vertex that is the key.

Returns:

Wheter or not a alternating path from an unmatched vertex in the left partition to an unmatched vertex in the right partition exists.

Return type:

bool

classmethod _dfs_step(ver: collections.abc.Hashable, edges: dict[collections.abc.Hashable, tuple[collections.abc.Hashable]], matches: dict[collections.abc.Hashable, collections.abc.Hashable], distances: dict[collections.abc.Hashable, int | float]) → bool

Completes the DFS step of Hopcroft-Karp. I.e.:

Adds all edges on every path with the minimal path length to matches if they would be in the symmetric difference of matches and the set of edges on the union of the paths.

Parameters:
  • ver (Hashable) – The set of verticies in the left partition of the bipartite graph.

  • edges (dict[Hashable, tuple[Hashable]]) – The edges in the bipartite graph. (As the edges are bidirectional they are expected to be given in this format: Dictionary with keys being the vertices in the left partition and values being tuples of verticies in the right partition.)

  • matches (dict[Hashable, Hashable]) – The current matching of verticies in the left partition with the ones in the right partition.

  • distances (dict[Hashable, Union[int, float]]) – The reference to the dictionary mapping verticies of the left partition to the minimal number of verticies in the left partition that will be passed on a path from an unmatched vertex of the left partition to the vertex that is the key. The values will be replaced with float(“inf”) to mark already visited vertices.

Returns:

Wheter or not a path from the unmatched vertex ver in the left partition to an unmatched vertex in the right partition could still exist.

Return type:

bool

classmethod _hopcroft_karp(verticies1: set[collections.abc.Hashable], verticies2: set[collections.abc.Hashable], edges: dict[collections.abc.Hashable, tuple[collections.abc.Hashable]]) → int

Implementation of Hopcroft-Karp. I.e.:

Finds how maximal number of edges with the property that no two edges share an endpoint (and startpoint) in the given bipartite graph.

Note that verticies1 and verticies2 have to be disjoint.

Parameters:
  • verticies1 (set[Hashable]) – The set of verticies in the left partition of the bipartite graph.

  • verticies2 (set[Hashable]) – The set of verticies in the right partition of the bipartite graph.

  • edges (dict[Hashable, tuple[Hashable]]) – The edges in the bipartite graph. (As the edges are bidirectional they are expected to be given in this format: Dictionary with keys being the vertices in the left partition and values being tuples of verticies in the right partition.)

Returns:

The number of edges.

Return type:

int

__ne__(other: ld_list | list[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.ld_dict | ld_list]) → bool

Returns whether or not self and other not considered to be equal.

(Returns not self.__eq__(other) if the return type is bool. See ld_list.__eq__() for more details on the comparison.)

Parameters:

other (ld_list | list[JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list]) – The list/ container/ ld_list self is compared to.

Returns:

Whether or not self and other are not considered equal. If other is of the wrong type return the NotImplemented singleton instead.

Return type:

bool

append(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.ld_dict | ld_list) → None

Append the item to the given ld_list self.

The given value is expanded. If it is assimilated by self all items that would be added by this are added.

Parameters:

value (JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The new value.

Return type:

None

extend(value: list[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.ld_dict | ld_list]) → None

Append the items in value to the given ld_list self.

The given values are expanded. If any are assimilated by self all items that would be added by this are added.

Parameters:

value (list[JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dcit | ld_list]) – The new values.

Return type:

None

to_native_python() → list[hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | hermes.model.types.ld_container.NATIVE_LD_CONTAINER]

Return a native python version of this object where all ld_container are replaced by lists and dicts.

Returns:

The native python version of self.

Return type:

list[BASIC_TYPE | TIME_TYPE | NATIVE_LD_CONTAINER]

compact(context: list[hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT | str] | hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT | str | None = None) → hermes.model.types.ld_container.COMPACTED_JSON_LD_VALUE

Returns the compacted version of the given ld_list using its context only if none was supplied. The returned object is of the form {“@context”: the_context, container_type: compacted_content}.

Parameters:

context (list[JSON_LD_CONTEXT_DICT | str] | JSON_LD_CONTEXT_DICT | str | None) – The context to use for the compaction. If None the context of self is used.

Returns:

The compacted version of selfs JSON-LD representation.

Return type:

COMPACTED_JSON_LD_VALUE

classmethod is_ld_list(ld_value: Any) → bool

Returns wheter the given value is considered to be possible of representing an ld_list.

I.e. if ld_value is of the form [{container_type: […]}] where container_type is ‘@set’, ‘@list’ or ‘@graph’.

Parameters:

ld_value (Any) – The value that is checked.

Returns:

Wheter or not ld_value could represent an ld_list.

Return type:

bool

classmethod is_container(value: Any) → bool

Returns wheter the given value is considered to be possible of representing an json-ld container.

I.e. if ld_value is of the form {container_type: […]} where container_type is ‘@set’, ‘@list’ or ‘@graph’.

Parameters:

ld_value (Any) – The value that is checked.

Returns:

Wheter or not ld_value could represent a json-ld container.

Return type:

bool

classmethod from_list(value: list[hermes.model.types.ld_container.JSON_LD_VALUE | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE], *, parent: hermes.model.types.ld_dict.ld_dict | ld_list | None = None, key: str | None = None, context: str | hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT | list[str | hermes.model.types.ld_container.JSON_LD_CONTEXT_DICT] | None = None, container_type: str = '@set') → Self

Creates a ld_list from the given list with the given parent, key, context and container_type.

Note that only container_type ‘@set’ is valid for key ‘@type’.

Further more note that if parent would assimilate the values in value no new ld_list is created and the given values are appended to parent instead and parent is returned.

Parameters:
  • value (list[JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE]) – The list of values the ld_list should be created from.

  • parent (ld_dict | ld_list | None) – The parent container of the new ld_list. If value is assimilated by parent druing JSON-LD expansion parent is extended by value and parent is returned.

  • key (str | None) – The key into the inner most parent container representing a dict of the new ld_list.

  • context (str | JSON_LD_CONTEXT_DICT | list[str | JSON_LD_CONTEXT_DICT] | None) – The context for the new list (it will also inherit the context of parent). Note that this context won’t be added to parent if value is assimilated by parent and parent is returned.

  • container_type (str) – The container type of the new list valid are ‘@set’, ‘@list’ and ‘@graph’. If value is assimilated by parent and parent is returned the given container_type won’t affect the container type of parent. Also note that only ‘@set’ is valid if key is ‘@type’.

Returns:

The new ld_list build from value or if value is assimilated by parent, parent extended by value.

Return type:

ld_list

Raises:

ValueError – If key is ‘@type’ and container_type is not ‘@set’.

classmethod get_item_list_from_container(ld_value: dict[str, list[Any]]) → list[Any]

Returns the item list from a container, the given ld_value, (i.e. {container_type: item_list}). Only ‘@set’, ‘@list’ and ‘@graph’ are valid container types.

Parameters:

ld_value (dict[str, list[Any]]) – The container whose item list is to be returned.

Returns:

The list the container holds.

Return type:

list[Any]

Raises:
  • ValueError – If the item_container is not a dict.

  • ValueError – If the container_type is not exactly one of ‘@set’, ‘@list’ and ‘@graph’.

  • ValueError – If the item_list is no list.