Example #1
0
def load_file(location, fallback=None, as_yaml=False):
    '''
    Unpickle either *json* or *yaml* from a file

    :param location: path where to unpickle from
    :param fallback: data to return in case of unpickle failure
    :param as_yaml: read as *yaml* instead of *json*
    :return: unpickled data from ``location``
    '''
    with load_struct(read_file(location), fallback=fallback,
                     as_yaml=as_yaml) as data:
        return data
Example #2
0
def load_file(location, fallback=None, as_yaml=False):
    '''
    Unpickle either *json* or *yaml* from a file

    :param location: path where to unpickle from
    :param fallback: data to return in case of unpickle failure
    :param as_yaml: read as *yaml* instead of *json*
    :return: unpickled data from ``location``
    '''
    with load_struct(
        read_file(location), fallback=fallback, as_yaml=as_yaml
    ) as data:
        return data
Example #3
0
def sly(cont):
    with load_struct(cont, as_yaml=True) as res:
        return res
Example #4
0
def slj(cont):
    with load_struct(cont, as_yaml=False) as res:
        return res
Example #5
0
def test_load_struct_wrong_input_data():
    for fd in [None, True, '', {}, (), 23]:
        for asy in [True, False]:
            with load_struct(fd, as_yaml=asy) as t:
                assert t is None
Example #6
0
def test_load_struct_yaml():
    for ty in ['', 'a', 'a:', 'a: b', 'a: [b, c]']:
        with load_struct(ty, as_yaml=True) as t:
            assert t == y_load(ty)
Example #7
0
def test_load_struct_json():
    for tj in ['""', '"a"', '{}', '{"a": "b"}']:
        with load_struct(tj, as_yaml=False) as t:
            assert t == j_load(tj)
Example #8
0
def test_load_struct_yaml_fishy():
    for fy in ['"', ':', '>>>']:
        with load_struct(fy, fallback='wrong', as_yaml=True) as t:
            assert t == 'wrong'
Example #9
0
def test_load_struct_json_fishy():
    for fj in ['"', 'a', '\n', '"a":', '{"a": "b",}']:
        with load_struct(fj, fallback='wrong', as_yaml=False) as t:
            assert t == 'wrong'
Example #10
0
def test_load_struct_fallback():
    for fb in [None, {}, 1, 'whatever', ('a', 'b')]:
        for asy in [True, False]:
            with load_struct(False, fallback=fb, as_yaml=asy) as t:
                assert t == fb