hermes.model.types.ld_dict

Classes

ld_dict

An JSON-LD container resembling a dict.

Module Contents

class hermes.model.types.ld_dict.ld_dict(data: list[dict[str, hermes.model.types.ld_container.EXPANDED_JSON_LD_VALUE]], *, parent: ld_dict | hermes.model.types.ld_list.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 dict. See also ld_container

data_dict

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

Type:

dict[str, EXPANDED_JSON_LD_VALUE]

_NO_DEFAULT

(class attribute) A type used as a placeholder to represent “no default”.

Type:

type[str]

__getitem__(key: str) → hermes.model.types.ld_list.ld_list

Get the item with the given key in as a ld_list.

If self contains no key, value pair with the given key, then an empty list is added as its value and returned.

Parameters:

key (str) – The key (compacted or expanded) to the item.

Returns:

The ld_list item at the key.

Return type:

ld_list

__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 | ld_dict | hermes.model.types.ld_list.ld_list) → None

Set the item at the given key to the given value or delete it if value is None. The given value is expanded.

Parameters:
  • key (str) – The key at which the item is set.

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

Return type:

None

__delitem__(key: str) → None

Delete the key, value pair with the given value pair.

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:

key (str) – The key (expanded or compacted) of the key, value pair that is deleted.

Return type:

None

__contains__(key: str) → bool

Returns whether or not self contains a key, value pair with the given key.

Parameters:

key (str) – The key for which it is checked if a key, value pair is contained in self.

Returns:

Whether or not self contains a key, value pair with the given key.

Return type:

bool

__eq__(other: ld_dict | dict[str, hermes.model.types.ld_container.JSON_LD_VALUE | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | ld_dict | hermes.model.types.ld_list.ld_list]) → bool

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

If other is not an ld_dict, it is converted first.

If an id check is possible return its result otherwise:

For each key, value pair its value is compared to the value with the same key in other.

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

Parameters:

other (ld_dict | dict[str, JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list]) – The dict/ ld_dict 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

__ne__(other: ld_dict | dict[str, hermes.model.types.ld_container.JSON_LD_VALUE | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | ld_dict | hermes.model.types.ld_list.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_dict.__eq__() for more details on the comparison.)

Parameters:

other (ld_dict | dict[str, JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list]) – The dict/ ld_dict 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

__bool__() → bool

Returns the truth value self would have if it was a normal dict.

I.e. returns true if no key, value pair is in self.

Returns:

The truth value of self.

Return type:

bool

setdefault(key: str, default: hermes.model.types.ld_container.JSON_LD_VALUE | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | ld_dict | hermes.model.types.ld_list.ld_list) → hermes.model.types.ld_list.ld_list

Get the value for the given key if self has a value for the key. Otherwise set the value for key to default and then return the value at key in self.

Parameters:
  • key (str) – The key at which the value is returned.

  • default (JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list) – The value that is set at key in self if there is no value for key in self.

Returns:

The value at key in self (if no value at key in self, it is set to default first).

Return type:

ld_list

emplace(key: str) → None

Emplace the value at key in self (it is set to an empty list) if there is no value yet.

Parameters:

key (str) – The key at which the value in self is emplaced.

Return type:

None

get(key: str, default: Any = _NO_DEFAULT) → hermes.model.types.ld_list.ld_list | Any

Get the item with the given key in as a ld_list using the build in get.

If a KeyError is raised, return the default or reraise it if no default is given.

Parameters:

key (str) – The key (compacted or expanded) to the item.

Returns:

The ld_list item at the key.

Return type:

ld_list

Raises:

KeyError – If __getitem__(key)() raised a KeyError and default isn’t set.

update(other: ld_dict | dict[str, hermes.model.types.ld_container.JSON_LD_VALUE | hermes.model.types.ld_container.BASIC_TYPE | hermes.model.types.ld_container.TIME_TYPE | ld_dict | hermes.model.types.ld_list.ld_list]) → None

Set the items at the given keys to the given values or delete it if value is None by using build in set.

Parameters:

other (ld_dict | dict[str, JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE | ld_dict | ld_list]) – The key, value pairs giving the new values and their keys.

Return type:

None

keys() → collections.abc.KeysView[str]

Return the keys of the key, value pairs of self.

Returns:

The keys of the values in self.

Return type:

KeysView[str]

compact_keys() → collections.abc.Iterator[str]

Return an iterator of the compacted keys of the key, value pairs of self.

Returns:

An iterator over the compacted keys in self.

Return type:

Iterator[str]

items() → collections.abc.Generator[tuple[str, hermes.model.types.ld_list.ld_list], None, None]

Return an generator of tuples of keys and their values in self.

Returns:

A Generator over all key, value pairs in self.

Return type:

Generator[tuple[str, ld_list], None, None]

property ref: dict[Literal['@id'], str]

…})

Returns:

The minimal JSON_LD object referencing self.

Return type:

dict[Literal[“@id”], str]

Raises:

KeyError – If self has no value for “@id”.

Type:

Return the dict used to reference this object by its id. (Its form is {“@id”

to_native_python() → dict[str, 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:

dict[str, BASIC_TYPE | TIME_TYPE | NATIVE_LD_CONTAINER]

classmethod from_dict(value: dict[str, hermes.model.types.ld_container.NATIVE_LD_CONTAINER], *, parent: ld_dict | hermes.model.types.ld_list.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, ld_type: str | list[str] | None = None) → ld_dict

Creates a ld_dict from the given dict with the given parent, key, context and ld_type.

Uses the expansion of the JSON-LD Processor and not the one of ld_container.

Parameters:
  • value (dict[str, NATIVE_LD_CONTAINER]) – The dict of values the ld_dict should be created from.

  • parent (ld_dict | ld_list | None) – The parent container of the new ld_list.

  • 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 dict (it will also inherit the context of parent).

  • ld_type (str | list[str] | None) – Additional value(s) for the new dict.

Returns:

The new ld_dict build from value.

Return type:

ld_dict

classmethod is_ld_dict(ld_value: Any) → bool

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

I.e. if ld_value is a list containing a dict containing none of the keys “@set”, “@graph”, “@list” and “@value” and not only the key “@id”.

Parameters:

ld_value (Any) – The value that is checked.

Returns:

Wheter or not ld_value could represent an expanded json-ld dict.

Return type:

bool

classmethod is_json_dict(ld_value: Any) → bool

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

I.e. if ld_value is a dict containing none of the keys “@set”, “@graph”, “@list” and “@value” and not only the key “@id”.

Parameters:

ld_value (Any) – The value that is checked.

Returns:

Wheter or not ld_value could represent an expanded json-ld dict.

Return type:

bool