Example #1
0
def test_cache_outputs():
    """Test the capture of stdout."""
    path = 'myvars.pkl'
    cell = """a = 1;print(a+1)"""
    
    user_ns = {}
    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)
    
    def ip_push(vars):
        user_ns.update(vars)
    
    cache(cell, path, vars=['a'], verbose=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1
    
    # Capture stdout.
    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()

    user_ns = {}
    cache(cell, path, vars=['a'], verbose=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1
    
    sys.stdout = old_stdout
    
    # Check that stdout contains the print statement of the cached cell.
    assert mystdout.getvalue() == '2\n'
    
    os.remove(path)
Example #2
0
def test_cache_exception():
    """Check that, if an exception is raised during the cell's execution,
    the pickle file is not written."""
    path = 'myvars.pkl'
    cell = """a = 1;b = 1/0"""

    user_ns = {}

    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)

    def ip_push(vars):
        user_ns.update(vars)

    cache(cell,
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    assert not os.path.exists(path), os.remove(path)
Example #3
0
def test_cache_1():
    path = 'myvars.pkl'
    cell = """a = 1"""
    
    user_ns = {}
    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)
    
    def ip_push(vars):
        user_ns.update(vars)
    
    cache(cell, path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1
    
    # We modify the variable in the namespace,
    user_ns['a'] = 2
    # and execute the cell again. The value should be loaded from the pickle
    # file.
    cache("""a = 2""", path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1
    
    # Now, we force the cell's execution.
    cache("""a = 2""", path, vars=['a'], force=True, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 2
    
    # Now, we prevent the cell's execution.
    user_ns['a'] = 0
    cache("""a = 3""", path, vars=['a'], force=False, read=True,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 2
    
    os.remove(path)
Example #4
0
def test_cache_1():
    path = 'myvars.pkl'
    cell = """a = 1"""

    user_ns = {}

    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)

    def ip_push(vars):
        user_ns.update(vars)

    cache(cell,
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    # We modify the variable in the namespace,
    user_ns['a'] = 2
    # and execute the cell again. The value should be loaded from the pickle
    # file.
    cache("""a = 2""",
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    # Now, we force the cell's execution.
    cache("""a = 2""",
          path,
          vars=['a'],
          force=True,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 2

    # Now, we prevent the cell's execution.
    user_ns['a'] = 0
    cache("""a = 3""",
          path,
          vars=['a'],
          force=False,
          read=True,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 2

    os.remove(path)
Example #5
0
def test_cache_fail_1():
    """Fails when saving inexistent variables."""
    path = 'myvars.pkl'
    cell = """a = 1"""
    
    user_ns = {}
    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)
    
    def ip_push(vars):
        user_ns.update(vars)
    
    cache(cell, path, vars=['a', 'b'],
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    
    os.remove(path)
Example #6
0
def test_cache_exception():
    """Check that, if an exception is raised during the cell's execution,
    the pickle file is not written."""
    path = 'myvars.pkl'
    cell = """a = 1;b = 1/0"""
    
    user_ns = {}
    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)
    
    def ip_push(vars):
        user_ns.update(vars)
    
    cache(cell, path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1
    
    assert not os.path.exists(path), os.remove(path)
Example #7
0
def test_cache_outputs():
    """Test the capture of stdout."""
    path = 'myvars.pkl'
    cell = """a = 1;print(a+1)"""

    user_ns = {}

    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)

    def ip_push(vars):
        user_ns.update(vars)

    cache(cell,
          path,
          vars=['a'],
          verbose=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    # Capture stdout.
    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()

    user_ns = {}
    cache(cell,
          path,
          vars=['a'],
          verbose=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    sys.stdout = old_stdout

    # Check that stdout contains the print statement of the cached cell.
    assert mystdout.getvalue() == '2\n'

    os.remove(path)
Example #8
0
def test_cache_fail_1():
    """Fails when saving inexistent variables."""
    path = 'myvars.pkl'
    cell = """a = 1"""

    user_ns = {}

    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)

    def ip_push(vars):
        user_ns.update(vars)

    cache(cell,
          path,
          vars=['a', 'b'],
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)

    os.remove(path)
Example #9
0
def test_cache_1():
    path = 'myvars.pkl'
    cell = """a = 1"""

    user_ns = {}

    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)

    def ip_push(vars):
        user_ns.update(vars)

    cache(cell,
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    # We modify the variable in the namespace,
    user_ns['a'] = 2
    # and execute the cell again. The value should be loaded from the pickle
    # file. Note how we did not change cell contents
    cache("""a = 1""",
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    # changing  the cell will trigger reload
    # file. Note how we did not change cell contents
    cache("""a = 2""",
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 2

    #store 1 again
    user_ns['a'] = 1
    cache("""a = 1""",
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    #hack the md5 so code change does not retrigger
    with open(path, 'rb') as op:
        data = pickle.load(op)
    data['_cell_md5'] = hashlib.md5("""a = 2""".encode()).hexdigest()
    with open(path, 'wb') as op:
        pickle.dump(data, op)
    #ensure we don't rerun
    user_ns['a'] = 2
    # and execute the cell again. The value should be loaded from the pickle
    # file. Note how we did not change cell contents
    cache("""a = 1""",
          path,
          vars=['a'],
          force=False,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 1

    # Now, we force the cell's execution.
    cache("""a = 2""",
          path,
          vars=['a'],
          force=True,
          read=False,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 2

    # Now, we prevent the cell's execution.
    user_ns['a'] = 0
    cache("""a = 3""",
          path,
          vars=['a'],
          force=False,
          read=True,
          ip_user_ns=user_ns,
          ip_run_cell=ip_run_cell,
          ip_push=ip_push)
    assert user_ns['a'] == 2

    os.remove(path)
Example #10
0
def test_cache_1():
    path = 'myvars.pkl'
    cell = """a = 1"""
    
    user_ns = {}
    def ip_run_cell(cell):
        exec_(cell, {}, user_ns)
    
    def ip_push(vars):
        user_ns.update(vars)
    
    cache(cell, path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1
    
    # We modify the variable in the namespace,
    user_ns['a'] = 2
    # and execute the cell again. The value should be loaded from the pickle
    # file. Note how we did not change cell contents
    cache("""a = 1""", path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1

    # changing  the cell will trigger reload
    # file. Note how we did not change cell contents
    cache("""a = 2""", path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 2
     
    #store 1 again
    user_ns['a'] = 1 
    cache("""a = 1""", path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    #hack the md5 so code change does not retrigger
    with open(path, 'rb') as op:
        data = pickle.load(op)
    data['_cell_md5'] = hashlib.md5("""a = 2""".encode()).hexdigest()
    with open(path, 'wb') as op:
        pickle.dump(data, op)
    #ensure we don't rerun
    user_ns['a'] = 2
    # and execute the cell again. The value should be loaded from the pickle
    # file. Note how we did not change cell contents
    cache("""a = 1""", path, vars=['a'], force=False, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 1

    # Now, we force the cell's execution.
    cache("""a = 2""", path, vars=['a'], force=True, read=False,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 2
    
    # Now, we prevent the cell's execution.
    user_ns['a'] = 0
    cache("""a = 3""", path, vars=['a'], force=False, read=True,
          ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
    assert user_ns['a'] == 2
    
    os.remove(path)