コード例 #1
0
def test_persistence():
    """ Checking if the data persists correctly when saving and loading back
    """
    if six.PY2:
        raise SkipTest("Pickling MultiContext instances is broken on Python 2.")
    d1 = DataContext(name = 'test_context1',
                     subcontext = {'a':1, 'b':2})
    d2 = DataContext(name = 'test_context2',
                     subcontext = {'foo':100, 'bar':200, 'baz':300})
    m = MultiContext(d1, d2, name='test_mc')

    f = BytesIO()
    m.save(f)
    f.seek(0, 0)
    new_m = MultiContext.load(f)

    assert m.name == new_m.name

    # Test keys of new_m
    assert set(new_m.keys()) == set(m.keys())

    # Check values
    assert new_m['a'] == m['a']
    assert new_m['b'] == m['b']
    assert new_m['foo'] == m['foo']
    assert new_m['bar'] == m['bar']

    # Check contexts
    assert new_m.subcontexts == m.subcontexts
コード例 #2
0
def test_persistence():
    """ Checking if the data persists correctly when saving and loading back
    """
    d1 = DataContext(name="test_context1", subcontext={"a": 1, "b": 2})
    d2 = DataContext(name="test_context2", subcontext={"foo": 100, "bar": 200, "baz": 300})
    m = MultiContext(d1, d2, name="test_mc")

    f = StringIO()
    m.save(f)
    f.seek(0, 0)
    new_m = MultiContext.load(f)

    assert m.name == new_m.name

    # Test keys of new_m
    assert set(new_m.keys()) == set(m.keys())

    # Check values
    assert new_m["a"] == m["a"]
    assert new_m["b"] == m["b"]
    assert new_m["foo"] == m["foo"]
    assert new_m["bar"] == m["bar"]

    # Check contexts
    assert new_m.subcontexts == m.subcontexts
コード例 #3
0
    def test_keys(self):
        """ Tests to ensure 2 contexts that contain overlapping sets of keys
            appear to have one set of keys where each key is unique (i.e. a 'set')
        """

        d1 = DataContext(name="d1", subcontext={"a": 1, "b": 2})
        d2 = DataContext(name="d2", subcontext={"a": 3, "c": 4})
        m = MultiContext(d1, d2, name="m")

        sorted_keys = sorted(m.keys())
        self.assertEqual(sorted_keys, ["a", "b", "c"])
コード例 #4
0
    def test_keys(self):
        """ Tests to ensure 2 contexts that contain overlapping sets of keys
            appear to have one set of keys where each key is unique (i.e. a 'set')
        """

        d1 = DataContext(name='d1',subcontext={'a':1,'b':2})
        d2 = DataContext(name='d2',subcontext={'a':3,'c':4})
        m = MultiContext(d1, d2, name='m')

        sorted_keys = sorted(m.keys())
        self.assertEqual(sorted_keys, ['a', 'b', 'c'])
コード例 #5
0
    def test_keys(self):
        """ Tests to ensure 2 contexts that contain overlapping sets of keys
            appear to have one set of keys where each key is unique (i.e. a 'set')
        """

        d1 = DataContext(name='d1', subcontext={'a': 1, 'b': 2})
        d2 = DataContext(name='d2', subcontext={'a': 3, 'c': 4})
        m = MultiContext(d1, d2, name='m')

        sorted_keys = sorted(m.keys())
        self.assertEqual(sorted_keys, ['a', 'b', 'c'])
コード例 #6
0
def test_checkpoint():
    d1 = DataContext()
    d1['a'] = object()
    d2 = DataContext()
    d2['b'] = object()
    m = MultiContext(d1, d2)
    copy = m.checkpoint()
    assert copy is not m
    assert copy.subcontexts is not m.subcontexts
    assert len(copy.subcontexts) == len(m.subcontexts)
    for csc, msc in zip(copy.subcontexts, m.subcontexts):
        assert csc is not msc
        assert set(csc.keys()) == set(msc.keys())
        for key in msc.keys():
            assert csc[key] is msc[key]

    assert set(copy.keys()) == set(m.keys())
    assert copy['a'] is m['a']
    assert copy['b'] is m['b']
コード例 #7
0
def test_checkpoint():
    d1 = DataContext()
    d1['a'] = object()
    d2 = DataContext()
    d2['b'] = object()
    m = MultiContext(d1, d2)
    copy = m.checkpoint()
    assert copy is not m
    assert copy.subcontexts is not m.subcontexts
    assert len(copy.subcontexts) == len(m.subcontexts)
    for csc, msc in zip(copy.subcontexts, m.subcontexts):
        assert csc is not msc
        assert set(csc.keys()) == set(msc.keys())
        for key in list(msc.keys()):
            assert csc[key] is msc[key]

    assert set(copy.keys()) == set(m.keys())
    assert copy['a'] is m['a']
    assert copy['b'] is m['b']
コード例 #8
0
    def test_contexts_list_changes(self):
        """ Checking if change in items in contexts updates the multi-context
        """
        d1 = DataContext(name="test_context1", subcontext={"a": 1, "b": 2})
        d2 = DataContext(name="test_context2")

        m = MultiContext(*[d1, d2], **{"name": "test_mc"})
        self.assertTrue(len(m.keys()) == 2)

        # Add another context
        d3 = DataContext(name="test_context3", subcontext={"c": 3, "d": 4})
        m.subcontexts.append(d3)
        self.assertTrue(len(m.keys()) == 4)

        # Modify an existing context
        m.subcontexts[1].subcontext = {"cc": 5}
        self.assertTrue(len(m.keys()) == 5)

        # Remove a context
        m.subcontexts.pop(0)
        self.assertTrue(len(m.keys()) == 3)
コード例 #9
0
    def test_block_events(self):
        import numpy
        from codetools.blocks.api import Block

        context = DataContext(name="data")
        context.on_trait_change(self.event_listener, 'items_modified')
        context.defer_events = True
        context['a'] = 4
        context['b'] = numpy.array((1, 2, 3))
        context.defer_events = False

        self.assertEqual(self.event_count, 1)

        multi_context = MultiContext(context, name="multi")
        multi_context.on_trait_change(self.event_listener, 'items_modified')

        block = Block("c = a * b")
        block.execute(multi_context)

        # we expect one event from data context, one from multi context
        self.assertEqual(self.event_count, 3)
コード例 #10
0
ファイル: events_test_case.py プロジェクト: B-Rich/codetools
    def test_block_events(self):
        import numpy
        from codetools.blocks.api import Block

        context = DataContext(name="data")
        context.on_trait_change(self.event_listener, 'items_modified')
        context.defer_events = True
        context['a'] = 4
        context['b'] = numpy.array((1,2,3))
        context.defer_events = False

        self.assertEqual(self.event_count, 1)

        multi_context = MultiContext(context, name="multi")
        multi_context.on_trait_change(self.event_listener, 'items_modified')

        block = Block("c = a * b")
        block.execute(multi_context)

        # we expect one event from data context, one from multi context
        self.assertEqual(self.event_count, 3)
コード例 #11
0
def test_persistence():
    """ Checking if the data persists correctly when saving and loading back
    """
    d1 = DataContext(name='test_context1', subcontext={'a': 1, 'b': 2})
    d2 = DataContext(
        name='test_context2', subcontext={'foo': 100,
                                          'bar': 200,
                                          'baz': 300})
    m = MultiContext(d1, d2, name='test_mc')

    f = StringIO()
    m.save(f)
    f.seek(0, 0)
    new_m = MultiContext.load(f)

    assert m.name == new_m.name

    # Test keys of new_m
    assert set(new_m.keys()) == set(m.keys())

    # Check values
    assert new_m['a'] == m['a']
    assert new_m['b'] == m['b']
    assert new_m['foo'] == m['foo']
    assert new_m['bar'] == m['bar']

    # Check contexts
    assert new_m.subcontexts == m.subcontexts
コード例 #12
0
def test_checkpoint_nested():
    d1 = DataContext()
    d1["a"] = object()
    d2 = DataContext()
    d2["b"] = object()
    m1 = MultiContext(d1, d2)
    m = MultiContext(m1)
    copy = m.checkpoint()
    assert copy is not m
    assert copy.subcontexts is not m.subcontexts
    assert len(copy.subcontexts) == len(m.subcontexts)
    csc1 = copy.subcontexts[0]
    msc1 = m.subcontexts[0]
    for csc, msc in zip(csc1.subcontexts, msc1.subcontexts):
        assert csc is not msc
        assert set(csc.keys()) == set(msc.keys())
        for key in msc.keys():
            assert csc[key] is msc[key]

    assert set(copy.keys()) == set(m.keys())
    assert copy["a"] is m["a"]
    assert copy["b"] is m["b"]
コード例 #13
0
    def test_contexts_list_changes(self):
        """ Checking if change in items in contexts updates the multi-context
        """
        d1 = DataContext(name = 'test_context1',
                         subcontext = {'a':1, 'b':2})
        d2 = DataContext(name = 'test_context2')

        m = MultiContext(*[d1,d2], **{'name': 'test_mc'})
        self.assertTrue(len(list(m.keys())) == 2)

        # Add another context
        d3 = DataContext(name = 'test_context3',
                         subcontext = {'c':3, 'd':4})
        m.subcontexts.append(d3)
        self.assertTrue(len(list(m.keys())) == 4)

        # Modify an existing context
        m.subcontexts[1].subcontext = {'cc': 5}
        self.assertTrue(len(list(m.keys())) == 5)

        # Remove a context
        m.subcontexts.pop(0)
        self.assertTrue(len(list(m.keys())) == 3)
コード例 #14
0
    def test_set_rebind_between_contexts(self):
        """ Can we rebind variables between contained contexts?
        """

        class C(DataContext):
            f = Any

            def __init__(self, f):
                super(C, self).__init__(f=f)

            def allows(self, value, name):
                return self.f(value, name)

        # `allows' predicates
        all = lambda v, n: True
        none = lambda v, n: False
        positive = lambda v, n: v > 0
        negative = lambda v, n: v < 0

        # Make a multi-context where the top context only accepts positive
        # numbers and the next one accepts anything. For robustness, add some
        # noise below.
        multi = MultiContext(
            C(positive), C(all), C(all), C(none), C(negative))  # Noise
        [upper, lower] = multi.subcontexts[0:2]

        for a, b in [(3, 8), (3, -8), (-3, 8), (-3, -8)]:

            # Bind and rebind 'x'
            multi['x'] = a
            multi['x'] = b

            # 'x' should have the latter binding
            assert multi['x'] == b

            # 'x' should live in the upper context if it's positive, else in
            # the lower one
            if multi['x'] > 0:
                self.assertTrue('x' in upper)
            else:
                self.assertTrue('x' in lower)
コード例 #15
0
    def test_contexts_list_changes(self):
        """ Checking if change in items in contexts updates the multi-context
        """
        d1 = DataContext(name='test_context1', subcontext={'a': 1, 'b': 2})
        d2 = DataContext(name='test_context2')

        m = MultiContext(* [d1, d2], **{'name': 'test_mc'})
        self.assertTrue(len(list(m.keys())) == 2)

        # Add another context
        d3 = DataContext(name='test_context3', subcontext={'c': 3, 'd': 4})
        m.subcontexts.append(d3)
        self.assertTrue(len(list(m.keys())) == 4)

        # Modify an existing context
        m.subcontexts[1].subcontext = {'cc': 5}
        self.assertTrue(len(list(m.keys())) == 5)

        # Remove a context
        m.subcontexts.pop(0)
        self.assertTrue(len(list(m.keys())) == 3)
コード例 #16
0
 def context_factory(self, *args, **kw):
     """ Return the type of context we are testing.
     """
     return MultiContext(DataContext(*args, **kw))
コード例 #17
0
 class ContextWrapper(HasTraits):
     context = MultiContext({"a": 100, "b": 200, "c": 50})
     editor = ContextShellEditor()
     traits_view = View(
         Group(Item('context', editor=editor, width=400, height=400)))