API¶
- class forestwalker.Walker(root: Any)¶
Bases:
objectA Walker is a pointer to a particular node of the tree, or possible a missing child node. It also remembers the path to the node.
- obj: Any¶
The tree node.
MissingValueif it is a missing node.
- raise_error(msg) NoReturn¶
Raise a
WalkerErroron the current node with the given error message.
- is_null() bool¶
Tests if the current node is
None(JSONnull).
- is_str() bool¶
Tests if the current node is a string.
- is_int() bool¶
Tests if the current node is an integer.
- is_number() bool¶
Tests if the current node is an integer or float.
- is_bool() bool¶
Tests if the current node is a Boolean value.
- is_missing() bool¶
Tests if the current node is a missing child.
- is_present() bool¶
Tests if the current node is not a missing child.
- is_array() bool¶
Tests if the current node is an array.
- is_object() bool¶
Tests if the current node is an object (dictionary).
- expect_present() Self¶
Raises an error if the current node is a missing child. Returns itself.
- as_str(default: str | None = None) str¶
If the current node is a string, returns its value. If it is missing and default is given, returns default. Otherwise raises
WalkerError.
- as_int(default: int | None = None) int¶
If the current node is an integer, returns its value. If it is missing and default is given, returns default. Otherwise raises
WalkerError.
- as_float(default: float | None = None) float¶
If the current node is a float, returns its value. If it is an integer, it is cast to a float. If it is missing and default is given, returns default. Otherwise raises
WalkerError.
- as_bool(default: bool | None = None) bool¶
If the current node is a Boolean, returns its value. If it is missing and default is given, returns default. Otherwise raises
WalkerError.
- as_enum(enum: Type[E], default: E | None = None) E¶
If the current node is a string, returns its value cast to the given enumeration type (descendant of
Enum). If the node is missing and default is given, returns default. Otherwise raisesWalkerError.
- as_optional_str() str | None¶
If the current node is a string, returns its value. If it is missing, returns
None. Otherwise raisesWalkerError.
- as_optional_int() int | None¶
If the current node is an integer, returns its value. If it is missing, returns
None. Otherwise raisesWalkerError.
- as_optional_float() float | None¶
If the current node is a float, returns its value. If it is an integer, it is cast to a float. If it is missing, returns
None. Otherwise raisesWalkerError.
- as_optional_bool() bool | None¶
If the current node is a Boolean, returns its value. If it is missing, returns
None. Otherwise raisesWalkerError.
- array_values() Iterator[WalkerInArray]¶
Produces an iterator over an array node, which yields
WalkerInArrayobjects for the elements of the array. If the node is not an array, raisesWalkerError.
- object_values() Iterator[WalkerInObject]¶
Produces an iterator over attributes of an object (dictionary), which yields
WalkerInObjectobjects for the values of the attributes. If the node is not an object, raisesWalkerError.
- object_items() Iterator[Tuple[str, WalkerInObject]]¶
Produces an iterator over attributes of an object (dictionary), which yields pairs of the attribute’s name and a
WalkerInObjectobject for the attribute’s value. If the node is not an object, raisesWalkerError.
- enter_object() ObjectWalker¶
Produces an
ObjectWalkerfor an object (dictionary), which allows indexing of the object’s attributes and checking which attributes are missing. If the node is not an object, raisesWalkerError.
- default_to(default) Walker¶
If the current node is missing, make the walker point to default instead. Returns itself, so it is possible to write e.g.
walker.default_to([]).array_values().
- context() str¶
Construct a path fragment for the current node.
- class forestwalker.WalkerInArray(obj: Any, parent: Walker, index: int)¶
Bases:
WalkerA
Walkerreferring to an array item. Never constructed directly, useWalker.array_items()to obtain it.- index: int¶
Position of the item in the array (zero-based).
- class forestwalker.WalkerInObject(obj: Any, parent: Walker, key: str)¶
Bases:
WalkerA
Walkerreferring to an object attribute (dictionary item). Never constructed directly, useWalker.object_values(),Walker.object_items(), or indexing inObjectWalker()to obtain it.- key: str¶
Key of the attribute.
- unexpected() NoReturn¶
Raises a
WalkerErrorcomplaining that this key was not expected.
- class forestwalker.ObjectWalker(obj: Any, parent: Walker)¶
Bases:
WalkerA
Walkerfor inspecting an object (dictionary). In addition to the default walker, it allows indexing of attribute by the square brackets operator. It also remembers which attributes were referenced this way and when used as a context manager, it complains upon exit from the context if the object contains unreferenced attributes.Never constructed directly, use
Walker.enter_object()to obtain it.- referenced_keys: Set[str]¶
A set of keys of referenced attributes.
- assert_no_other_keys() None¶
Checks if the object has attributes not accessed by the
[]operator. Raises anWalkerErrorif there are any.