Created
October 19, 2020 13:31
-
-
Save jobel-code/2cf3de663317497e755b5b049e3e5d63 to your computer and use it in GitHub Desktop.
Load yaml file from filepath
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
import yaml | |
import os | |
def load_yaml(filepath: str) -> dict: | |
"""Loads a yaml file. | |
Can be used as stand alone script by | |
:params filepath: file path to the yaml file to be loaded. | |
:usage: | |
`load_yaml.py --filepath /file/path/to/filename.yaml` | |
:return: a dictionary object | |
""" | |
# assert os.path.isdir(os.path.dirname(filepath)) | |
assert os.path.isfile(filepath) | |
if not os.path.isfile(filepath): | |
print(f"`filepath` missing = `{filepath}`, ensure `filepath` exist or " | |
"if standalone `--filepath /file/path/filename.yaml` argument is given.") | |
return None | |
else: | |
with open(filepath, 'r') as file_descriptor: | |
try: | |
yaml_data = yaml.full_load(file_descriptor) | |
except Exception as msg: | |
print(f'File `{filepath}` loading error. \n {msg}') | |
else: | |
return yaml_data | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(add_help=True) | |
parser.add_argument('--filepath', action="store", dest="filepath", type= str, default = True) | |
# parser.add_argument('-f ', action="store", dest="filepath", type=str, default=False) | |
args = parser.parse_args() | |
load_yaml(args.filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment