-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…yaml ✨ Add data loader for YAML
- Loading branch information
Showing
8 changed files
with
215 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from pystreamapi.loaders.__csv.__csv_loader import csv | ||
from pystreamapi.loaders.__json.__json_loader import json | ||
from pystreamapi.loaders.__xml.__xml_loader import xml | ||
from pystreamapi.loaders.__yaml.__yaml_loader import yaml | ||
|
||
__all__ = [ | ||
'csv', | ||
'json', | ||
'xml' | ||
'xml', | ||
'yaml' | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
try: | ||
import yaml as yaml_lib | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Please install the yaml_loader extra dependency to use the yaml loader." | ||
) from exc | ||
from collections import namedtuple | ||
|
||
from pystreamapi.loaders.__lazy_file_iterable import LazyFileIterable | ||
from pystreamapi.loaders.__loader_utils import LoaderUtils | ||
|
||
|
||
def yaml(src: str, read_from_src=False) -> LazyFileIterable: | ||
""" | ||
Loads YAML data from either a path or a string and converts it into a list of namedtuples. | ||
Args: | ||
src (str): Either the path to a YAML file or a YAML string. | ||
read_from_src (bool): If True, src is treated as a YAML string. If False, src is treated as | ||
a path to a YAML file. | ||
Returns: | ||
list: A list of namedtuples, where each namedtuple represents an object in the YAML. | ||
""" | ||
if read_from_src: | ||
return LazyFileIterable(lambda: __load_yaml_string(src)) | ||
path = LoaderUtils.validate_path(src) | ||
return LazyFileIterable(lambda: __load_yaml_file(path)) | ||
|
||
|
||
def __load_yaml_file(file_path): | ||
"""Load a YAML file and convert it into a list of namedtuples""" | ||
# skipcq: PTC-W6004 | ||
with open(file_path, mode='r', encoding='utf-8') as yamlfile: | ||
src = yamlfile.read() | ||
if src == '': | ||
return [] | ||
data = yaml_lib.safe_load(src) | ||
return __convert_to_namedtuples(data) | ||
|
||
|
||
def __load_yaml_string(yaml_string): | ||
"""Load YAML data from a string and convert it into a list of namedtuples""" | ||
data = yaml_lib.safe_load(yaml_string) | ||
return [] if data is None else __convert_to_namedtuples(data) | ||
|
||
|
||
def __convert_to_namedtuples(data, name='Item'): | ||
"""Convert YAML data to a list of namedtuples""" | ||
if isinstance(data, dict): | ||
fields = list(data.keys()) | ||
Item = namedtuple(name, fields) | ||
return Item(**{k: __convert_to_namedtuples(v, k) for k, v in data.items()}) | ||
if isinstance(data, list): | ||
return [__convert_to_namedtuples(item, name) for item in data] | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.