hermes.model.path

Module Contents

Classes

ContextPathGrammar

The pyparsing grammar for ContextGrammar paths.

ContextPath

This class is used to access the different contexts.

Functions

set_in_dict(target, key, value, kwargs)

Attributes

_log

hermes.model.path._log
hermes.model.path.set_in_dict(target: dict, key: str, value: object, kwargs)
class hermes.model.path.ContextPathGrammar

The pyparsing grammar for ContextGrammar paths.

key
index
field
path
classmethod parse(text: str) pyparsing.ParseResults

Parse a ContextPath string representation into its individual tokens.

Parameters:

text – The path to parse.

Returns:

The pyparsing.ParseResult.

class hermes.model.path.ContextPath(item: str | int | List[str | int], parent: ContextPath | None = 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, use ContextPath.parse().

property parent: ContextPath | None

Accessor to the parent node.

property path: List[ContextPath]

Get the whole path from the root as list of items.

merge_strategies
classmethod init_merge_strategies()
__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()

__repr__() str

Return repr(self).

__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) ContextPath | None
_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) ContextPath | None
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: Dict[str, Any] | List, value: Any, tags: dict | None = 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.