hermes.model.path
Contents
hermes.model.path
Module Contents
Classes
The pyparsing grammar for ContextGrammar paths. |
|
This class is used to access the different contexts. |
Functions
|
Attributes
- class hermes.model.path.ContextPath(item: str | int | t.List[str | int], parent: Optional[ContextPath] = None)
This class is used to access the different contexts.
On the one hand, the class allows you to define and manage paths. You can simply build them up like follows:
>>> path = ContextPath('spam')['eggs'][1]['ham']
will result in a path like spam.eggs[1].ham.
- hint ::
The paths are idenpendent from any context. You can create and even re-use them independently for different contexts.
To construct wildcard paths, you can use the ‘*’ as accessor.
If you need a shortcut for building paths from a list of accessors, you can use
ContextPath.make()
. To parse the string representation, useContextPath.parse()
.- property parent: Optional[ContextPath]
Accessor to the parent node.
- property path: List[ContextPath]
Get the whole path from the root as list of items.
- __getitem__(item: str | int) ContextPath
Create a sub-path for the given item.
- __str__() str
Get the string representation of the path. The result is parsable by
ContextPath.parse()
- __eq__(other: ContextPath) bool
This match includes semantics for wildcards. Items that access ‘*’ will automatically match everything (except for None).
- __contains__(other: ContextPath) bool
Check whether other is a true child of this path.
- new() Any
Create a new instance of the container this node represents.
For this to work, the node need to have at least on child node derive (e.g., by using
self["child"]
).
- static _get_item(target: dict | list, path: ContextPath) Optional[ContextPath]
- _find_in_parent(target: dict, path: ContextPath) Any
- _find_setter(target: dict | list, path: ContextPath, value: Any = None, **kwargs) Callable
- _set_item(target: dict | list, path: ContextPath, value: Any, **kwargs) Optional[ContextPath]
- resolve(target: list | dict, create: bool = False, query: Any = None)
Resolve a given path relative to a given target.
The method will incrementally try to resolve the entries in the _target.path. It stops when the requested item was found or when the resolution could not be completed. If you set create to true, the method tries to create the direct target that contains the selected node.
- Parameters
target – Container to resolve node in.
create – Flags whether missing containers should be created.
query –
- Returns
The method returns a tuple with the following values: - The path to the last item that could be resolved (e.g., the container of the requested element). - The container for the path from the first return value. - The rest of the path that could not be resolved.
- get_from(target: dict | list) Any
Expand the path and return the referenced data from a concrete container.
- Parameters
target – The list or dict that this path points into.
- Returns
The value stored at path.
- update(target: t.Dict[str, t.Any] | t.List, value: Any, tags: Optional[dict] = None, **kwargs)
Update the data stored at the path in a concrete container.
How this method actually behaves heavily depends on the active MergeStrategy for the path.
- Parameters
target – The dict inside which the value should be stored.
value – The value to store.
tags – Dictionary containing the tags for all stored values.
kwargs – The tag attibutes for the new value.
- classmethod make(path: Iterable[str | int]) ContextPath
Convert a list of item accessors into a ContextPath.
- Parameters
path – The items in the order of access.
- Returns
A ContextPath that reference the selected value.
- classmethod parse(path: str) ContextPath
Parse a string representation of a ContextPath into a proper object.
- Parameters
path – The path to parse.
- Returns
A new ContextPath that references the selected path.