Ejemplo n.º 1
0
    def test_pair(self):

        term_var = TermVar('foo')
        term_this = TermThis()
        m = comm.SegmentationMark(10)

        with self.assertRaises(TypeError):
            TermPair(42, term_var)

        with self.assertRaises(TypeError):
            TermPair('bar', 42)

        with self.assertRaises(TypeError):
            TermPair({}, 42)

        #--

        term_pair = TermPair('bar', term_var)
        value = term_pair.compute({'foo': m, 'zz': 100})
        self.assertIs(type(value), comm.Record)
        self.assertEqual(value.content, {'bar': m})

        #--

        term_pair = TermPair('bar', term_this)

        with self.assertRaises(AssertionError):
            term_pair.compute({'bar': m, 'zz': 100})

        value = term_pair.compute({'__this__': m})
        self.assertIs(type(value), comm.Record)
        self.assertEqual(value.content, {'bar': m})
Ejemplo n.º 2
0
    def test_segmark(self):

        ms = comm.SegmentationMark(23)
        md = comm.Record({'foo': 1, 'bar': 2, 'baz': 3})

        c = sync_backend.ConditionSegmark('d')
        self.assertTrue(c.test(ms))
        self.assertEqual(c.locals, {'d': 23})

        c = sync_backend.ConditionSegmark('d')
        self.assertFalse(c.test(md))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionSegmark('d', ['tree'])
        self.assertFalse(c.test(ms))
        self.assertEqual(c.locals, {})

        ms['tree'] = -1
        ms['foobar'] = -2

        c = sync_backend.ConditionSegmark('d', ['tree'])
        self.assertTrue(c.test(ms))
        self.assertEqual(c.locals, {'d': 23, 'tree': -1})

        c = sync_backend.ConditionSegmark('d', ['tree'], 'rest')
        self.assertTrue(c.test(ms))
        self.assertEqual(c.locals, {
            'd': 23,
            'tree': -1,
            'rest': comm.Record({'foobar': -2})
        })
Ejemplo n.º 3
0
    def test_var(self):

        with self.assertRaises(TypeError):
            TermVar(4)

        exp = TermVar('foo')

        with self.assertRaises(RuntimeError):
            exp.compute({'bar': 5})

        m = comm.SegmentationMark(10)
        value = exp.compute({'foo': m, 'zz': 100})
        self.assertEqual(id(value), id(m))
Ejemplo n.º 4
0
    def test_var_expand(self):

        with self.assertRaises(TypeError):
            TermVarExpand(4)

        exp = TermVarExpand('foo')

        with self.assertRaises(RuntimeError):
            exp.compute({'bar': 5})

        def _test(m):
            pair = exp.compute({'foo': m, 'zz': 100})
            self.assertEqual(type(pair), comm.Record)
            self.assertEqual(pair.content, {'foo': m})

        _test(comm.SegmentationMark(10))
        _test(100)
Ejemplo n.º 5
0
    def test_data(self):

        ms = comm.SegmentationMark(23)
        m = comm.Record({'foo': 1, 'bar': 2, 'baz': 3})

        c = sync_backend.ConditionData()
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData()
        self.assertFalse(c.test(ms))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData([])
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData(['foo'])
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {'foo': 1})

        c = sync_backend.ConditionData(['tree'])
        self.assertFalse(c.test(m))
        self.assertEqual(c.locals, {})

        c = sync_backend.ConditionData(['foo'], 'rest')
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {
            'foo': 1,
            'rest': comm.Record({
                'bar': 2,
                'baz': 3
            })
        })

        c = sync_backend.ConditionData(['foo', 'bar', 'baz'], 'rest')
        self.assertTrue(c.test(m))
        self.assertEqual(c.locals, {
            'foo': 1,
            'bar': 2,
            'baz': 3,
            'rest': comm.Record()
        })
Ejemplo n.º 6
0
    def test_data_exp(self):

        t_var = TermVar('foo')
        t_pair = TermPair('bar', TermVar('baz'))
        t_this = TermThis()
        t_var_zzz = TermVar('zzz')

        fcode = '10'
        f = eval('lambda: ' + fcode)
        f.code = fcode

        m_sm_10_exp = SegmentationMarkExp(IntExp(f))

        m_sm_10 = comm.SegmentationMark(10)
        m_sm_4 = comm.SegmentationMark(4)

        m_rec_empty = comm.Record()
        m_rec_12 = comm.Record({'1': 'one', '2': 'two'})
        m_rec_3 = comm.Record({'3': 'three'})

        m_rec_empty_exp = RecordExp(m_rec_empty.content)
        m_rec_12_exp = RecordExp(m_rec_12.content)
        m_rec_3_exp = RecordExp(m_rec_3.content)

        with self.assertRaises(TypeError):
            DataExp(42)

        with self.assertRaises(TypeError):
            DataExp('abc')

        with self.assertRaises(TypeError):
            DataExp([t_var, 'ololo'])

        with self.assertRaises(TypeError):
            DataExp([t_var, t_this], 42)

        # No init, terms: []
        dexp = DataExp([])
        result = dexp.compute({})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {})

        #---

        # Init: record, terms: []
        dexp = DataExp([], m_rec_12_exp)
        result = dexp.compute({})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {'1': 'one', '2': 'two'})

        #---

        # Init: record, terms: [records]
        dexp = DataExp([t_var, t_this], m_rec_empty_exp)
        result = dexp.compute({'__this__': m_rec_12, 'foo': m_rec_3})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {
            '1': 'one',
            '2': 'two',
            '3': 'three'
        })

        #---

        # Init: record, terms: [record, segmark]
        dexp = DataExp([t_var, t_this], m_rec_empty_exp)
        result = dexp.compute({'__this__': m_sm_4, 'foo': m_rec_3})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {'3': 'three', '__n__': 4})

        #---

        # Init: segmentation mark, terms: [records, segmark]
        dexp = DataExp([t_var, t_this, t_var_zzz], m_sm_10_exp)
        result = dexp.compute({
            '__this__': m_rec_12,
            'foo': m_rec_3,
            'zzz': m_sm_4
        })

        self.assertIs(type(result), comm.SegmentationMark)
        self.assertEqual(result.n, m_sm_10.n)

        (self.assertIn(i, result) for i in ('1', '2', '3'))

        #---

        # No init, terms: [records]
        dexp = DataExp([t_var, t_pair])
        result = dexp.compute({'baz': 100, 'foo': m_rec_3})

        self.assertIs(type(result), comm.Record)
        self.assertEqual(result.content, {'3': 'three', 'bar': 100})

        #---

        dexp = DataExp([t_var, t_this, t_var_zzz])

        # Non-record type in a union.
        with self.assertRaises(RuntimeError):
            dexp.compute({'__this__': m_sm_10, 'foo': 42, 'zzz': m_sm_4})

        # Two segmentation marks in a union.
        with self.assertRaises(RuntimeError):
            dexp.compute({'__this__': m_sm_10, 'foo': m_rec_3, 'zzz': m_sm_4})

        # No init, segmentation mark term
        result = dexp.compute({
            '__this__': m_rec_12,
            'foo': m_rec_3,
            'zzz': m_sm_4
        })

        self.assertIs(type(result), comm.SegmentationMark)
        self.assertEqual(result.n, m_sm_4.n)

        (self.assertIn(i, result) for i in ('1', '2', '3'))
Ejemplo n.º 7
0
        n = msg['chunks'].pop()

        out_msg['lst'] = msg['lst'][:n]
        msg['lst'] = msg['lst'][n:]
        del out_msg['chunks']

        return ('continue', {0: [out_msg]}, msg)


def c_Reduce(m1, m2):
    '1MU'
    m1['sum'] += m2['sum']
    return ('partial', {}, m1)


__input__ = {'in': [], '_in': []}

import random
import communication as comm

__input__['in'].append(
    comm.Record({
        'lst': [random.random() for i in range(100)],
        '__nfrag__': 7
    }))
__input__['in'].append(comm.SegmentationMark(0))
__input__['_in'].append(comm.SegmentationMark(0))

import astrakahn
astrakahn.start()
Ejemplo n.º 8
0
}
'''

#------------------------------------------------------------------------------

if __name__ == '__main__':

    import astrakahn
    import communication as comm

    __input__ = {
        'a': [
            comm.Record({'A': 1}),
            comm.Record({'A': 2}),
            comm.Record({'A': 3}),
            comm.SegmentationMark(1),
            comm.Record({'C': 1}),
            comm.Record({'C': 2}),
            comm.Record({'C': 3}),
            comm.SegmentationMark(0),
        ],
        'b': [
            comm.Record({'B': 1}),
            comm.Record({'B': 2}),
            comm.SegmentationMark(1),
            comm.Record({'B': 3}),
            comm.Record({'B': 4}),
            comm.SegmentationMark(0),
        ],
    }