Best Python code snippet using avocado_python
test_core.py
Source:test_core.py  
...3import midware.core as core4@pytest.fixture5def nested_dict():6    return {'a': {'b': 0}, 'c': 1}7def test_get_in(nested_dict):8    assert core.get_in(nested_dict, ('a')) == {'b': 0}9    assert core.get_in(nested_dict, ('a', 'b')) == 010    assert core.get_in(nested_dict, ('c')) == 111    assert core.get_in(nested_dict, ('a', 'd')) == None12    assert core.get_in(nested_dict, ('d')) == None13def test_get_in_default(nested_dict):14    assert core.get_in(nested_dict, ('a', 'b')) == 015    assert core.get_in(nested_dict, ('c', 'd')) == None16    assert core.get_in(nested_dict, ('a', 'd')) == None17    assert core.get_in(nested_dict, ('a', 'd'), default=0) == 018def test_assoc_in():19    d = {}20    core.assoc_in(d, ('a'), 0)21    assert d['a'] == 022    with pytest.raises(TypeError):23        core.assoc_in(d, ('a', 'b'), 0)24    core.assoc_in(d, ('b', 'c'), 1)25    assert d['b']['c'] == 126def test_identity():27    assert core.identity(1) == 128    assert core.identity('a') == 'a'29    assert core.identity(None) == None30def add_two(x):31    return x + 232def add_three(x):33    return x + 334def test_compose():35    assert core.compose(add_two, add_three)(1) == 636def test_compose_order():37    assert core.compose(sum, add_two, add_three)([1, 2, 3]) == 1138@pytest.fixture39def verbose(scope='function'):40    core._VERBOSE_MODE = True41    yield42    core._VERBOSE_MODE = False43def test_print_inwards(verbose, capsys):44    core._print_inwards('abc')45    out, err = capsys.readouterr()46    assert out == 'abc--->\n'47    assert err == ''48def test_print_outwards(verbose, capsys):49    core._print_outwards('xyz')50    out, err = capsys.readouterr()51    assert out == '<---xyz\n'52    assert err == ''53import os.path as fs54def test_mw_from_cm_enter(tmpdir):55    tmpfile = fs.join(tmpdir, 'tmp.txt')56    with open(tmpfile, 'w') as f:57        print('abc', file=f)58    ks = ('file', 'desc')59    mw = core.mw_from_cm('test_mw', open, ks, {}, file=tmpfile)60    lines = None61    def handler(ctx):62        with core.get_in(ctx, ks) as f:63            assert f.readlines() == ['abc\n']64        return ctx65    ctx = mw(handler)({})66def test_mw_from_cm_exit(tmpdir):67    tmpfile = fs.join(tmpdir, 'tmp.txt')68    with open(tmpfile, 'w') as f:69        print('abc', file=f)70    ks = ('file', 'desc')71    mw = core.mw_from_cm('test_mw', open, ks, {}, file=tmpfile)72    ctx = mw(core.identity)({})73    with pytest.raises(ValueError):74        with core.get_in(ctx, ks) as f:75            f.readlines()76def test_mw_from_cm_exit(tmpdir, verbose, capsys):77    tmpfile = fs.join(tmpdir, 'tmp.txt')78    with open(tmpfile, 'w') as f:79        print('abc', file=f)80    ks = ('file', 'desc')81    mw = core.mw_from_cm('test_cm_mw', open, ks, {}, file=tmpfile)82    ctx = mw(core.identity)({})83    out, err = capsys.readouterr()84    assert out == 'test_cm_mw--->\n<---test_cm_mw\n'85    assert err == ''86@core.middleware('wrap_add')87def wrap_add(ctx):88    amount = ctx['amount']..._toolz.py
Source:_toolz.py  
...30DAMAGE.31"""32import operator33from functools import reduce34def get_in(keys, coll, default=None, no_default=False):35    """36    NB: This is a straight copy of the get_in implementation found in37        the toolz library (https://github.com/pytoolz/toolz/). It works38        with persistent data structures as well as the corresponding39        datastructures from the stdlib.40    Returns coll[i0][i1]...[iX] where [i0, i1, ..., iX]==keys.41    If coll[i0][i1]...[iX] cannot be found, returns ``default``, unless42    ``no_default`` is specified, then it raises KeyError or IndexError.43    ``get_in`` is a generalization of ``operator.getitem`` for nested data44    structures such as dictionaries and lists.45    >>> from pyrsistent import freeze46    >>> transaction = freeze({'name': 'Alice',47    ...                       'purchase': {'items': ['Apple', 'Orange'],48    ...                                    'costs': [0.50, 1.25]},49    ...                       'credit card': '5555-1234-1234-1234'})50    >>> get_in(['purchase', 'items', 0], transaction)51    'Apple'52    >>> get_in(['name'], transaction)53    'Alice'54    >>> get_in(['purchase', 'total'], transaction)55    >>> get_in(['purchase', 'items', 'apple'], transaction)56    >>> get_in(['purchase', 'items', 10], transaction)57    >>> get_in(['purchase', 'total'], transaction, 0)58    059    >>> get_in(['y'], {}, no_default=True)60    Traceback (most recent call last):61        ...62    KeyError: 'y'63    """64    try:65        return reduce(operator.getitem, keys, coll)66    except (KeyError, IndexError, TypeError):67        if no_default:68            raise...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
