Skip to main content
Version: 0.18.21

YAMLHandler

class great_expectations.core.yaml_handler.YAMLHandler#

Facade class designed to be a lightweight wrapper around YAML serialization.

For all YAML-related activities in Great Expectations, this is the entry point.

Note that this is meant to be library agnostic - the underlying implementation does not matter as long as we fulfill the following contract:

  • load

  • dump

Typical usage example:

simple_yaml: str = '''
name: test
class_name: test_class
module_name: test.test_class
'''
yaml_handler = YAMLHandler()
res: dict = yaml_handler.load(simple_yaml)
example_dict: dict = dict(abc=1)
yaml_handler.dump(example_dict)

dump(data: dict, stream: Optional[Union[_io.TextIOWrapper, _io.StringIO, pathlib.Path]] = None, **kwargs) str | None#

Converts a Python dictionary into a YAML string.

Dump code has been adopted from: Example.Html

>>> data = {'foo': 'bar'}
>>> yaml_str = yaml_handler.dump(data)
>>> print(yaml_str)
foo:
bar:

Parameters
  • data – The dictionary to serialize into a Python object.

  • stream – The output stream to modify. If not provided, we default to io.StringIO.

  • kwargs – Additional key-word arguments to pass to underlying yaml dump method.

Returns

If no stream argument is provided, the str that results from _handler.dump(). Otherwise, None as the _handler.dump() works in place and will exercise the handler accordingly.

load(stream: _io.TextIOWrapper | str) dict[str, typing.Union[typing.Dict[str, typing.Union[typing.Dict[str, ForwardRef('JSONValues')], typing.List[ForwardRef('JSONValues')], str, int, float, bool, NoneType]], typing.List[typing.Union[typing.Dict[str, ForwardRef('JSONValues')], typing.List[ForwardRef('JSONValues')], str, int, float, bool, NoneType]], str, int, float, bool, NoneType]]#

Converts a YAML input stream into a Python dictionary.

Example:

import pathlib
yaml_handler = YAMLHandler()
my_file_str = pathlib.Path("my_file.yaml").read_text()
dict_from_yaml = yaml_handler.load(my_file_str)

Parameters

stream – The input stream to read in. Although this function calls ruamel's load(), we use a slightly more restrictive type-hint than ruamel (which uses Any). This is in order to tightly bind the behavior of the YamlHandler class with expected YAML-related activities of Great Expectations.

Returns

The deserialized dictionary form of the input stream.