files.py 701 Bytes
Newer Older
Matteo's avatar
Matteo committed
1
2
3
4
5
import json
import yaml


def get_file_content(file_name: str, format: str) -> dict:
Matteo's avatar
Matteo committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    """
    Parameters
    ----------
    file_name : str
        the path to the file to read
    format : str
        the format of the file to read (yaml or json)
    
    Raises
    ------
    ValueError
        if the format is not supported
    
    Returns
    -------
    dict
        the parsed content of the file
    """
Matteo's avatar
Matteo committed
24
25
26
27
28
29
30
31
32
33

    with open(file_name) as fd:
        if format == "yaml":
            content = yaml.safe_load(fd)
            return content
        elif format == "json":
            content = json.load(fd)
        else:
            raise ValueError("Format not supported")

Matteo's avatar
Matteo committed
34
    return content