vivyd.data.Container#
- class Container(*, name, info)[source]#
Bases:
Iterable[tuple[str,Any]]A base class for hierarchical containers that can hold other containers or general data objects.
- Parameters:
name (str) – A name for the container. This can be used to identify the container in a hierarchical structure.
info (dict[str, Any]) – A dictionary containing arbitrary metadata about the container. This can include any relevant information that describes the container or its contents.
Tip
You can easily iterate over the entries in a container, the same way you would iterate over a dictionary. For example, if
cis a container,for key, entry in c: print(f"Key: {key}, Entry: {entry}")
Attributes#
- Container.name: str#
The name of the container.
- Container.info: dict[str, Any]#
A dictionary containing arbitrary metadata about the container.
- Container.contents: dict[str, Any]#
A dictionary that holds the contents of the container.
Methods#
- Container.add(key, value)[source]#
Add an entry to the container.
- Parameters:
key (str) – The key for the entry.
value (Any) – The value for the entry.
- Raises:
ValueError – If an entry with the specified key already exists in the container.
Tip
The method supports chainable calls, so you can add multiple contents in a single statement. For example:
container.add(content1).add(content2).add(content3)
- Container.keys()[source]#
- Returns:
A list of keys for the entries in this container.
- Return type:
list[str]
- Container.tree(indent=2)[source]#
- Parameters:
indent (int, optional) – The number of spaces to use for each level of indentation in the tree representation. Default is 2.
- Returns:
A string representing the hierarchical tree structure of the container and its contents.
- Return type:
str
- Container.print_tree()[source]#
Print the hierarchical tree of container entries, as defined by
tree().
- Container.to_json(*, indent=2, ensure_ascii=False)[source]#
- Parameters:
indent (int, optional) – The number of spaces to use for each level of indentation in the JSON output. Default is 2.
ensure_ascii (bool, optional) – Whether to escape non-ASCII characters in the JSON output. Default is
False.
- Returns:
A JSON string representing the container and its contents.
- Return type:
str
- Container.print_json()[source]#
Print the container and its contents as formatted JSON.
- Parameters:
indent (int, optional) – The number of spaces to use for each level of indentation in the JSON output. Default is 2.
ensure_ascii (bool, optional) – Whether to escape non-ASCII characters in the JSON output. Default is
False.
- Container.get(key)[source]#
- Parameters:
key (str) – The key of the entry to retrieve from the container.
- Returns:
The value associated with the specified key in the container.
- Return type:
Any
Tip
A container object can be directly indexed to access its contents. In other words, the following two lines are equivalent for a container
c:c.get("k") c["k"]
where
"k"is the key of an entry in the container.