コード例 #1
0
def test_TaggedList_init_two_values_second_with_key():
    t = TaggedList([11, ('v2', 22)])
    assert t.astuples() == [(None, 11), ('v2', 22)]
    assert len(t) == 2
    assert t[0] == 11
    assert t[1] == 22
    assert t['v2'] == 22
コード例 #2
0
ファイル: rparser.py プロジェクト: aidan/pyrserv
    def xt_vector(self, lexeme):
        '''
        A vector is e.g. return when sending "list('abc','def')" to R. It can contain mixed
        types of data items.
        The binary representation of an XT_VECTOR is weird: a vector contains unknown number 
        of items, with possibly variable length. 
        The end of this REXP can only be detected by keeping track of how many bytes
        have been consumed (lexeme.length!) until the end of the REXP has been reached.
        '''
        finalLexpos = self.lexer.lexpos + lexeme.dataLength
        if DEBUG:
            print '%s     Vector-lexpos: %d, length %d, finished at: %d' % \
                (self.__ind, self.lexer.lexpos, lexeme.dataLength, finalLexpos)
        data = []
        while self.lexer.lexpos < finalLexpos:
            # convert single item arrays into atoms (via stripArray)
            data.append(self._stripArray(self._parseExpr().data))

        if lexeme.hasAttr and lexeme.attrTypeCode == XT_LIST_TAG:
            for tag, value in lexeme.attr:
                if tag == 'names':
                    # the vector has named items
                    data = TaggedList(zip(value, data))
                else:
                    if DEBUG:
                        print 'Warning: applying LIST_TAG "%s" on xt_vector not yet implemented' % tag
        return data
コード例 #3
0
def test_TaggedList_insert_with_key():
    t = TaggedList([11, ('v2', 22)])
    t.insert(0, x=1)
    assert len(t) == 3
    assert t.values == [1, 11, 22]
    assert t[0] == t['x'] == 1
コード例 #4
0
def test_TaggedList_init_emtpy():
    t = TaggedList()
    assert t.astuples() == []
    assert len(t) == 0
コード例 #5
0
def test_TaggedList_insert():
    t = TaggedList([11, ('v2', 22)])
    t.insert(0, 1)
    assert len(t) == 3
    assert t.values == [1, 11, 22]
コード例 #6
0
def test_TaggedList_append_with_key():
    t = TaggedList([11, ('v2', 22)])
    t.append(v3=33)
    assert len(t) == 3
    assert t.values == [11, 22, 33]
    assert t['v3'] == 33
コード例 #7
0
def test_TaggedList_append():
    t = TaggedList([11, ('v2', 22)])
    t.append(33)
    assert len(t) == 3
    assert t.values == [11, 22, 33]
コード例 #8
0
def test_TaggedList_init_one_value_with_key():
    t = TaggedList([('v1', 11)])
    assert t.astuples() == [('v1', 11)]
    assert len(t) == 1
    assert t[0] == 11
    assert t['v1'] == 11
コード例 #9
0
def test_TaggedList_init_one_value():
    t = TaggedList([11])
    assert t.astuples() == [(None, 11)]
    assert len(t) == 1
    assert t[0] == 11
コード例 #10
0
ファイル: test_rparser.py プロジェクト: aidan/pyrserv
 ('c(1, 2)', array([1.0, 2.0])),
 ('as.integer(c(1, 2))', array([1, 2], dtype=numpy.int32)),
 # single boolean value:
 ('TRUE', True),
 # a boolean vector:
 ('c(TRUE, FALSE, TRUE)', array([True, False, True])),
 # String vector:
 ('c("abc", "defghi")', array(["abc", "defghi"])),
 ('seq(1, 5)', array(range(1, 6), dtype=numpy.int32)),
 ('polyroot(c(-39.141,151.469,401.045))',
  array([0.1762039 + 1.26217745e-29j, -0.5538897 - 1.26217745e-29j])),
 # An explicit R list with only one item remains a list on the python side:
 ('list("otto")', ["otto"]),
 ('list("otto", "gustav")', ["otto", "gustav"]),
 # tagged lists:
 ('list(husband="otto")', TaggedList([("husband", "otto")])),
 ('list(husband="otto", wife="erna")',
  TaggedList([("husband", "otto"), ("wife", "erna")])),
 ('list(n="Fred", no_c=2, c_ages=c(4,7))',
  TaggedList([("n", "Fred"), ("no_c", 2.), ("c_ages", array([4., 7.]))])),
 # tagged array:
 ('c(a=1.,b=2.,c=3.)', asTaggedArray(array([1., 2., 3.]), ['a', 'b', 'c'])),
 # tagged single item array should remain an array on the python side in order to preserve the tag:
 ('c(a=1)', asTaggedArray(array([1.]), ['a'])),
 # multi-dim array (internally also a tagged array) gets translated into a shaped numpy array:
 ('array(1:20, dim=c(4, 5))', shaped_array(range(1, 21), numpy.int32,
                                           (4, 5))),
 #
 #('x<-1:20; y<-x*2; lm(y~x)',                ????),
 # Environment
 #('parent.env',                              [1,2]),