Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] render utils #6975

Merged
merged 6 commits into from
Jan 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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