Value
Absent ¶
Sentinel class to represent value absence.
ValuePath ¶
Bases: str
A path-like string for accessing nested mappings value.
__new__ ¶
__new__(path: str, *, separator: str | None = None) -> Self
Create a new ValuePath object from a path string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
path string of a value inside a mapping object. |
required |
separator
|
str | None
|
custom separator to use instead of the default. |
None
|
get_from ¶
get_from(mapping: Mapping[Any, V], default: V | None | Absent = ABSENT, *, raise_if_absent: bool = False) -> Any | None | Absent
Get the value describe by the path inside mapping object.
The value returns can be the sentinel ABSENT to differentiate real None value of default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[Any, V]
|
Dictionary or mapping object to lookup. |
required |
default
|
V | None | Absent
|
Default value if key not found. By default return the sentinel value |
ABSENT
|
raise_if_absent
|
bool
|
Flag to raise |
False
|
Note
Support both string and integer keys.
Raises:
| Type | Description |
|---|---|
ValueAbsentError
|
if parameter |
get ¶
get(mapping: Mapping[Any, V], default: V | None | Absent = ABSENT, *, raise_if_absent: bool = False) -> Any | None | Absent
Get the value describe by the path inside mapping object.
get is deprecated, use get_from instead.
The value returns can be the sentinel ABSENT to differentiate real None value of default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[Any, V]
|
Dictionary or mapping object to lookup. |
required |
default
|
V | None | Absent
|
Default value if key not found. By default return the sentinel value |
ABSENT
|
raise_if_absent
|
bool
|
Flag to raise |
False
|
Note
Support both string and integer keys.
Raises:
| Type | Description |
|---|---|
ValueAbsentError
|
if parameter |
Value ¶
Value(value_path: str, mapper: Mapper[Any, Any] | None = None)
Bases: Condition
Condition based on a value at a certain path in an event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_path
|
str
|
The path to the value in the event. |
required |
mapper
|
Mapper[Any, Any] | None
|
mapper to transform value before checking predicates on it. |
None
|
root
classmethod
¶
root() -> Self
Initialize value targeting condition over the event itself.
Examples:
Value.root().contains("a").check(my_dict)
# is equivalent to.
"a" in my_dict
check ¶
check(event: Event[V], *, raise_if_absent: bool = False) -> bool
Check the given event respect the value condition.
By default, the event fails the check if the value is absent, an error can be raised,
by setting the flag raise_if_absent.
Raises:
| Type | Description |
|---|---|
NoPredicateError
|
when no predicate has been set. |
ValueAbsentError
|
if a key define by the path is missing in event. |
equals ¶
equals(expected: Any) -> Self
Add value equals the expected value check to the condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
Any
|
The expected value. |
required |
match_regex ¶
match_regex(regex: Pattern[str]) -> Self
match_regex(regex: str, flags: RegexFlag | int = 0) -> Self
match_regex(regex: Pattern[str] | str, flags: RegexFlag | int = 0) -> Self
Add value matches the given regex pattern check to the condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex
|
Pattern[str] | str
|
regex to match (either a string or a Pattern) |
required |
flags
|
RegexFlag | int
|
regex flags; should bot be passed with a pattern. |
0
|
Raises:
| Type | Description |
|---|---|
TypeError
|
when passing flags whereas a |
one_of ¶
one_of(options: Container[Any]) -> Self
Add value is one of the given options check to the condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
Container[Any]
|
The container of options. |
required |
contains ¶
contains(*items: Any) -> Self
Add value contains all the given items to the condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Any
|
The items to check for. |
()
|
is_length ¶
is_length(length: int) -> Self
Add value has the specified size to the condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
The length expected. |
required |
match ¶
match(predicate: Predicate[Any]) -> Self
Add value matches the given predicate to the condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Predicate[Any]
|
The predicate to apply. |
required |
combine ¶
combine(*predicates: Predicate[Any]) -> Predicate[Any]
Combine all provided predicates into a single predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicates
|
Predicate[Any]
|
Predicates to combine. |
()
|