def test_execute(statements):
        executor = ThreadExecutor()
        loop = executor.event_loop

        for s, expected_state, expected_results in statements:
            statement = Statement(s, worksheet)
            statement._expected_state = expected_state
            statement._expected_results = expected_results
            statement._got_executing = False
            executor.add_statement(statement)

        def on_statement_executing(executor, statement):
            if hasattr(statement, '_got_state'):
                statement._out_of_order = True
            statement._got_executing = True

        def on_statement_complete(executor, statement):
            statement._got_state = statement.state
            statement._got_results = statement.results
            statement._out_of_order = False

        def on_complete(executor):
            loop.quit()

        def interrupt():
            executor.interrupt()

        global timed_out
        timed_out = False
        def timeout():
            global timed_out
            timed_out = True
            loop.quit()

        executor.sig_statement_executing.connect(on_statement_executing)
        executor.sig_statement_complete.connect(on_statement_complete)
        executor.sig_complete.connect(on_complete)

        if executor.compile():
            executor.execute()

            interrupt_source = threading.Timer(0.5, interrupt)
            interrupt_source.start()

            timeout_source = threading.Timer(1.0, timeout)
            timeout_source.start()
            loop.run()
            if timed_out:
                raise AssertionError("Interrupting ThreadExecutor failed")

            interrupt_source.cancel()
            timeout_source.cancel()

        for s in executor.statements:
            assert_equals(s._got_state, s._expected_state)
            assert_equals(s._got_results, s._expected_results)
            if s._out_of_order:
                raise AssertionError("ThreadExecutor sent 'sig_statement_executing' after 'sig_statement_complete'")
            if s._expected_state == Statement.INTERRUPTED and not s._got_executing:
                raise AssertionError("ThreadExecutor did not send 'sig_statement_executing' within timeout")
Пример #2
0
 def expect_result(text, result):
     s = Statement(text, worksheet)
     s.compile()
     s.execute()
     if s.error_message != None :
         raise Exception(s.error_message)
     if isinstance(result, basestring):
         assert_equals(s.results[0], result)
     else:
         assert_equals(s.results, result)
         pass
     pass
Пример #3
0
 def _disable_reinteract_output(self):
     self.statement = _Statement.get_current()
     if self.statement is not None:
         self.old_reinteract_output = self.statement.result_scope[
             'reinteract_output']
         if self._disable_output:
             self.statement.result_scope[
                 'reinteract_output'] = lambda *args: None
Пример #4
0
 def expect_result(text, result):
     s = Statement(text, worksheet)
     s.compile()
     s.execute()
     if s.error_message != None:
         raise Exception(s.error_message)
     if isinstance(result, basestring):
         assert_equals(s.results[0], result)
     else:
         assert_equals(s.results, result)
         pass
     pass
Пример #5
0
def _set_rcParams():
    """Add new values to rcParams and read in values from refigurerc file(s)."""
    
    _p.rcParams.validate.update({'refigure.printdpi': _p.matplotlib.rcsetup.validate_float,
                                 'refigure.disableoutput': _p.matplotlib.rcsetup.validate_bool,
                                 'refigure.keyboardcontrol': _p.matplotlib.rcsetup.validate_bool,
                                 'refigure.display': _p.matplotlib.rcsetup.ValidateInStrings('refigure.display', ['inline', 'side']),
                                 'refigure.toolbar': _p.matplotlib.rcsetup.validate_bool,
                                })
    _p.rcParams.update({'figure.figsize': [6.0, 4.5],
                        'figure.subplot.bottom': 0.12,
                        'refigure.printdpi': 300,
                        'refigure.disableoutput': False,
                        'refigure.keyboardcontrol': True,
                        'refigure.display': 'inline',
                        'refigure.toolbar': True,
                       })
    
    paths = ((_os.path.dirname(__file__), 'refigurerc'),
             (_os.path.expanduser('~'), '.matplotlib', 'refigurerc'))
    statement = _Statement.get_current()
    if statement is not None:
        paths += ((statement._Statement__worksheet.notebook.folder, 'refigurerc'),)
    for filename in (_os.path.join(*p) for p in paths):
        if _os.path.exists(filename):
            for line in file(filename, 'r'):
                stripline = line.split('#', 1)[0].strip()
                if not stripline:
                    continue
                key, val = [s.strip() for s in stripline.split(':', 1)]
                try:
                    _p.rcParams[key] = val
                except KeyError:
                    print "Warning: Invalid key %s in %s"%(key, filename)
                    print "         See rcParams.keys() for a list of valid keys."
                except Exception:
                    print "Warning: Bad value for %s: %s\n         in %s"%(key, val, filename)
Пример #6
0
def test_statement_0():
    from test_utils import assert_equals, adjust_environment
    adjust_environment()

    from reinteract.statement import Statement

    from reinteract.notebook import Notebook
    nb = Notebook()

    from reinteract.worksheet import Worksheet
    worksheet = Worksheet(nb)

    def expect_result(text, result):
        s = Statement(text, worksheet)
        s.compile()
        s.execute()
        if s.error_message != None :
            raise Exception(s.error_message)
        if isinstance(result, basestring):
            assert_equals(s.results[0], result)
        else:
            assert_equals(s.results, result)
            pass
        pass
    
    # A bare expression should give the repr of the expression
    expect_result("'a'", repr('a'))
    expect_result("1,2", repr((1,2)))

    # Print, on the other hand, gives the string form of the expression, with
    # one result object per output line
    expect_result("print 'a'", 'a')
    expect_result("print 'a', 'b'", ['a b'])
    expect_result("print 'a\\nb'", ['a','b'])

    # Test that we copy a variable before mutating it (when we can detect
    # the mutation)
    s1 = Statement("b = [0]", worksheet)
    s1.compile()
    s1.execute()
    s2 = Statement("b[0] = 1", worksheet, parent=s1)
    s2.compile()
    s2.execute()
    s3 = Statement("b[0]", worksheet, parent=s2)
    s3.compile()
    s3.execute()
    assert_equals(s3.results[0], "1")
    
    s2a = Statement("b[0]", worksheet, parent=s1)
    s2a.compile()
    s2a.execute()
    assert_equals(s2a.results[0], "0")

    # Test __reinteract_wrappers with an unrealistic example
    s1 = Statement("__reinteract_wrappers = [ lambda x: 2 ]", worksheet)
    s1.compile()
    s1.execute()
    s2 = Statement("1", worksheet, parent=s1)
    s2.compile()
    s2.execute()
    assert_equals(s2.results[0], "2")

    # Tests of catching errors
    s1 = Statement("b = ", worksheet)
    assert_equals(s1.compile(), False)
    assert s1.error_message is not None

    s1 = Statement("b", worksheet)
    assert_equals(s1.compile(), True)
    assert_equals(s1.execute(), False)
    assert s1.error_message is not None

    # Tests of 'from __future__ import...'
    s1 = Statement("from __future__ import division", worksheet)
    s1.compile()
    assert_equals(s1.future_features, ['division'])
    s2 = Statement("from __future__ import with_statement", worksheet, parent=s1)
    s2.compile()
    assert_equals(s2.future_features, ['division', 'with_statement'])

    s1 = Statement("import  __future__", worksheet) # just a normal import
    assert_equals(s1.future_features, None)

    # Advanced use of "context manager" protocol
    expect_result('from reinteract.statement import Statement; Statement.get_current() != None', repr(True))

    #--------------------------------------------------------------------------------------
    pass
Пример #7
0
 def _disable_reinteract_output(self):
     self.statement = _Statement.get_current()
     if self.statement is not None:
         self.old_reinteract_output = self.statement.result_scope['reinteract_output']
         if self._disable_output:
             self.statement.result_scope['reinteract_output'] = lambda *args: None
Пример #8
0
    def test_execute(statements):
        executor = ThreadExecutor()
        loop = executor.event_loop

        for s, expected_state, expected_results in statements:
            statement = Statement(s, worksheet)
            statement._expected_state = expected_state
            statement._expected_results = expected_results
            statement._got_executing = False
            executor.add_statement(statement)

        def on_statement_executing(executor, statement):
            if hasattr(statement, '_got_state'):
                statement._out_of_order = True
            statement._got_executing = True

        def on_statement_complete(executor, statement):
            statement._got_state = statement.state
            statement._got_results = statement.results
            statement._out_of_order = False

        def on_complete(executor):
            loop.quit()

        def interrupt():
            executor.interrupt()

        global timed_out
        timed_out = False

        def timeout():
            global timed_out
            timed_out = True
            loop.quit()

        executor.sig_statement_executing.connect(on_statement_executing)
        executor.sig_statement_complete.connect(on_statement_complete)
        executor.sig_complete.connect(on_complete)

        if executor.compile():
            executor.execute()

            interrupt_source = threading.Timer(0.5, interrupt)
            interrupt_source.start()

            timeout_source = threading.Timer(1.0, timeout)
            timeout_source.start()
            loop.run()
            if timed_out:
                raise AssertionError("Interrupting ThreadExecutor failed")

            interrupt_source.cancel()
            timeout_source.cancel()

        for s in executor.statements:
            assert_equals(s._got_state, s._expected_state)
            assert_equals(s._got_results, s._expected_results)
            if s._out_of_order:
                raise AssertionError(
                    "ThreadExecutor sent 'sig_statement_executing' after 'sig_statement_complete'"
                )
            if s._expected_state == Statement.INTERRUPTED and not s._got_executing:
                raise AssertionError(
                    "ThreadExecutor did not send 'sig_statement_executing' within timeout"
                )
Пример #9
0
def test_statement_0():
    from test_utils import assert_equals, adjust_environment
    adjust_environment()

    from reinteract.statement import Statement

    from reinteract.notebook import Notebook
    nb = Notebook()

    from reinteract.worksheet import Worksheet
    worksheet = Worksheet(nb)

    def expect_result(text, result):
        s = Statement(text, worksheet)
        s.compile()
        s.execute()
        if s.error_message != None:
            raise Exception(s.error_message)
        if isinstance(result, basestring):
            assert_equals(s.results[0], result)
        else:
            assert_equals(s.results, result)
            pass
        pass

    # A bare expression should give the repr of the expression
    expect_result("'a'", repr('a'))
    expect_result("1,2", repr((1, 2)))

    # Print, on the other hand, gives the string form of the expression, with
    # one result object per output line
    expect_result("print 'a'", 'a')
    expect_result("print 'a', 'b'", ['a b'])
    expect_result("print 'a\\nb'", ['a', 'b'])

    # Test that we copy a variable before mutating it (when we can detect
    # the mutation)
    s1 = Statement("b = [0]", worksheet)
    s1.compile()
    s1.execute()
    s2 = Statement("b[0] = 1", worksheet, parent=s1)
    s2.compile()
    s2.execute()
    s3 = Statement("b[0]", worksheet, parent=s2)
    s3.compile()
    s3.execute()
    assert_equals(s3.results[0], "1")

    s2a = Statement("b[0]", worksheet, parent=s1)
    s2a.compile()
    s2a.execute()
    assert_equals(s2a.results[0], "0")

    # Test __reinteract_wrappers with an unrealistic example
    s1 = Statement("__reinteract_wrappers = [ lambda x: 2 ]", worksheet)
    s1.compile()
    s1.execute()
    s2 = Statement("1", worksheet, parent=s1)
    s2.compile()
    s2.execute()
    assert_equals(s2.results[0], "2")

    # Tests of catching errors
    s1 = Statement("b = ", worksheet)
    assert_equals(s1.compile(), False)
    assert s1.error_message is not None

    s1 = Statement("b", worksheet)
    assert_equals(s1.compile(), True)
    assert_equals(s1.execute(), False)
    assert s1.error_message is not None

    # Tests of 'from __future__ import...'
    s1 = Statement("from __future__ import division", worksheet)
    s1.compile()
    assert_equals(s1.future_features, ['division'])
    s2 = Statement("from __future__ import with_statement",
                   worksheet,
                   parent=s1)
    s2.compile()
    assert_equals(s2.future_features, ['division', 'with_statement'])

    s1 = Statement("import  __future__", worksheet)  # just a normal import
    assert_equals(s1.future_features, None)

    # Advanced use of "context manager" protocol
    expect_result(
        'from reinteract.statement import Statement; Statement.get_current() != None',
        repr(True))

    #--------------------------------------------------------------------------------------
    pass