huni1023 / huni-utils

frequently used utilies for python
MIT License
2 stars 0 forks source link

read_yaml requires 'Loader' argument #1

Closed huni1023 closed 6 months ago

huni1023 commented 7 months ago

depends on PyYaml version, Loader argument is required

def read_yaml(yaml_dir: str) -> dict:
    r"""read yaml file"""
    _, file_extension = os.path.splitext(yaml_dir)
    assert file_extension == '.yaml', 'only yaml file is allowed'
    with open(yaml_dir, encoding='utf-8') as cfg:
        config = yaml.safe_load(cfg)
    return config
huni1023 commented 7 months ago

sample codes from other project

    with open(f'{BASE_DIR}/{pdf_target}/{cleaned_grade_str}{subject_eng}.yaml', 'rb') as f:
        try:
            config = yaml.load(f, Loader=yaml.FullLoader)
        except yaml.YAMLError as e:
            print("Parsing YAML string failed")
            print("Reason:", e.reason)
            print("At position: {0} with encoding {1}".format(e.position, e.encoding))
            print("Invalid char code:", e.character)
            raise ValueError