Skip to content

Commit

Permalink
modify: edit code - small modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
tanbro committed Apr 30, 2024
1 parent ded8373 commit 9f86e51
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ the actual URL to access is `http://$HOST:$PORT/sub_1/sub_1_1/xyz.yml`
When load [YAML][] string with include statement, the including files are default parsed into python objects. Thant is, if we call `yaml.dump()` on the object, what dumped is the parsed python object, and can not serialize the include statement itself.
To serialize the statement, we shall first create an `yaml_include.Constructor` object whose `autoload` is `False`:
To serialize the statement, we shall first create an `yaml_include.Constructor` object whose **`autoload` is `False`**:
```python
import yaml
Expand Down
11 changes: 6 additions & 5 deletions src/yaml_include/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ def __call__(self, loader: Union[_Loader, _CLoader], node: Node) -> Union[Data,
val: Union[_Scalar, Sequence, Mapping]
if is_yaml_scalar_node(node):
val = loader.construct_scalar(node)
if not isinstance(val, str):
if isinstance(val, str):
data = Data(val)
else:
raise TypeError(f"{type(val)}")
data = Data(val)
elif is_yaml_sequence_node(node):
val = loader.construct_sequence(node)
data = Data(val[0], sequence_params=val[1:])
Expand Down Expand Up @@ -240,7 +241,7 @@ def load(self, loader_type: Type[Union[_Loader, _CLoader]], data: Data) -> Any:
The function works as blow description:
* If there is a protocol/scheme, and no wildcard defined in YAML including,
``*args`` and ``**kwargs`` will be passed to :func:`fsspec.open`.
``*args`` and ``**kwargs`` will be passed to :func:`fsspec.open`.
Example:
The YAML
Expand All @@ -255,7 +256,7 @@ def load(self, loader_type: Type[Union[_Loader, _CLoader]], data: Data) -> Any:
yaml.load(f, Loader)
* If there is a protocol/scheme, and also wildcard defined in YAML including,
:attr:`.Data.sequence_params` and :attr:`.Data.mapping_params` of ``data`` will be passed to :func:`fsspec.open_files` as ``*args`` and ``**kwargs``
:attr:`.Data.sequence_params` and :attr:`.Data.mapping_params` of ``data`` will be passed to :func:`fsspec.open_files` as ``*args`` and ``**kwargs``
Example:
The YAML
Expand All @@ -271,7 +272,7 @@ def load(self, loader_type: Type[Union[_Loader, _CLoader]], data: Data) -> Any:
yaml.load(file, Loader)
* If there is no protocol/scheme, and no wildcard defined in YAML including,
:attr:`.Data.sequence_params` and :attr:`.Data.mapping_params` of ``data`` will be passed to :mod:`fsspec` file-system implementation's ``open`` function (derive from :meth:`fsspec.spec.AbstractFileSystem.open`) as ``*args`` and ``**kwargs``
:attr:`.Data.sequence_params` and :attr:`.Data.mapping_params` of ``data`` will be passed to :mod:`fsspec` file-system implementation's ``open`` function (derive from :meth:`fsspec.spec.AbstractFileSystem.open`) as ``*args`` and ``**kwargs``
* If there is no protocol/scheme, and also wildcard defined in YAML including, the situation is complex:
* If the include statement is in a positional-parameter form:
Expand Down
10 changes: 4 additions & 6 deletions src/yaml_include/representer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ class Representer:
def __call__(self, dumper: Dumper, data) -> Node:
if not isinstance(data, Data):
raise TypeError(f"{type(data)}")
tag = "!" + self.tag
if data.mapping_params:
kv_args = {"urlpath": data.urlpath}
kv_args.update(data.mapping_params)
return dumper.represent_mapping(f"!{self.tag}", kv_args)
return dumper.represent_mapping(tag, {**{"urlpath": data.urlpath}, **data.mapping_params})
if data.sequence_params:
pos_args = chain((data.urlpath,), data.sequence_params)
return dumper.represent_sequence(f"!{self.tag}", pos_args)
return dumper.represent_scalar(f"!{self.tag}", data.urlpath)
return dumper.represent_sequence(tag, chain((data.urlpath,), data.sequence_params))
return dumper.represent_scalar(tag, data.urlpath)

0 comments on commit 9f86e51

Please sign in to comment.