def test_to_yaml(self): m_file = os.path.join(tmp_dir, "movie_data") movie_string = _to_yaml(movie_data) assert "Rick Moranis" in movie_string _to_yaml(movie_data, filename=m_file) assert "Rick Moranis" in open(m_file).read() yaml = YAML() assert yaml.load(open(m_file)) == yaml.load(movie_string)
def to_yaml( self, filename: Union[str, PathLike] = None, default_flow_style: bool = False, encoding: str = "utf-8", errors: str = "strict", **yaml_kwargs, ): """ Transform the Box object into a YAML string. :param filename: If provided will save to file :param default_flow_style: False will recursively dump dicts :param encoding: File encoding :param errors: How to handle encoding errors :param yaml_kwargs: additional arguments to pass to yaml.dump :return: string of YAML (if no filename provided) """ return _to_yaml( self.to_dict(), filename=filename, default_flow_style=default_flow_style, encoding=encoding, errors=errors, **yaml_kwargs, )
def test_to_yaml_ruamel(self): movie_string = _to_yaml(movie_data, ruamel_attrs={"width": 12}) multiline_except = """ - name: Roger Rees imdb: nm0715953 role: Sheriff of Rottingham - name: Amy Yasbeck""" assert multiline_except in movie_string
def to_yaml(self, filename: str = None, default_flow_style: bool = False, encoding: str = 'utf-8', errors: str = 'strict', **yaml_kwargs): """ Transform the BoxList object into a YAML string. :param filename: If provided will save to file :param default_flow_style: False will recursively dump dicts :param encoding: File encoding :param errors: How to handle encoding errors :param yaml_kwargs: additional arguments to pass to yaml.dump :return: string of YAML or return of `yaml.dump` """ return _to_yaml(self.to_list(), filename=filename, default_flow_style=default_flow_style, encoding=encoding, errors=errors, **yaml_kwargs)