Skip to content

Commit

Permalink
[DOCS] render utils (#6975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilo59 committed Jan 27, 2023
1 parent 849af54 commit 031fdc3
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions great_expectations/render/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import locale
import re
import warnings
from typing import Any, List, Optional, Tuple, Union
from typing import Any, List, Optional, Sequence, Tuple, Union

import pandas as pd

Expand Down Expand Up @@ -125,15 +125,27 @@ def resource_key_passes_run_name_filter(resource_key, run_name_filter):
return run_name_filter.get("ne") != run_name


def substitute_none_for_missing(kwargs, kwarg_list):
@public_api
def substitute_none_for_missing(
kwargs: dict[str, Any], kwarg_list: Sequence[str]
) -> dict[str, Any]:
"""Utility function to plug Nones in when optional parameters are not specified in expectation kwargs.
Example:
Input:
kwargs={"a":1, "b":2},
kwarg_list=["c", "d"]
Args:
kwargs: A dictionary of keyword arguments.
kwargs_list: A list or sequence of strings representing all possible keyword parameters to a function.
Returns:
A copy of the original `kwargs` with missing keys from `kwarg_list` defaulted to `None`.
Output: {"a":1, "b":2, "c": None, "d": None}
```python
>>> result = substitute_none_for_missing(
... kwargs={"a":1, "b":2},
... kwarg_list=["c", "d"]
... )
... print(result)
{"a":1, "b":2, "c": None, "d": None}
```
This is helpful for standardizing the input objects for rendering functions.
The alternative is lots of awkward `if "some_param" not in kwargs or kwargs["some_param"] == None:` clauses in renderers.
Expand All @@ -147,9 +159,28 @@ def substitute_none_for_missing(kwargs, kwarg_list):


# NOTE: the method is pretty dirty
@public_api
def parse_row_condition_string_pandas_engine(
condition_string: str, with_schema: bool = False
) -> tuple:
) -> tuple[str, dict]:
"""Parses the row condition string into a pandas engine compatible format.
Args:
condition_string: A pandas row condition string.
with_schema: Return results in json schema format. Defaults to False.
Returns:
A tuple containing the template string and a `dict` of parameters.
```python
>>> template_str, params = parse_row_condition_string_pandas_engine("Age in [0, 42]")
>>> print(template_str)
"if $row_condition__0"
>>> params
{"row_condition__0": "Age in [0, 42]"}
```
"""
if len(condition_string) == 0:
condition_string = "True"

Expand Down

0 comments on commit 031fdc3

Please sign in to comment.