wow-inequalities/01-scripts/load_yaml.py
Marty Oehme 446db080f6
chore(code): Refactor yml loading code
Fix for new paths and restructure to have the from_yml and to_tsv
functions in the same data loading file.
Improved path behavior to resolve any paths to absolutes before
loading.
2023-12-07 13:04:29 +01:00

39 lines
950 B
Python

import sys
from typing import cast
import yaml
from pathlib import Path
def _get_all_yml(path: Path) -> list:
"""Returns list of all yml files."""
return list(path.rglob(r"**/*.y*ml"))
def _read_yml(path: Path) -> dict | None:
try:
with open(path, "r") as f:
return yaml.safe_load(f)
except FileNotFoundError as e:
print(e)
return None
def load(yml_path: Path | str) -> list[dict]:
"""Main data process routine.
Extracts all necessary data from yaml files returns it.
"""
contents = [_read_yml(source) for source in _get_all_yml(Path(yml_path))]
if not contents:
return []
contents = cast(list[dict], contents)
for study in contents:
del study["annotation"]
return contents
if __name__ == "__main__":
if len(sys.argv) == 2:
res = load(Path(sys.argv[1]))
print(res)
else:
print("Please provide path to yml files.")