def test_setitem_slice_invalid():
    values = (b'a', b'b', b'c')
    vec = ri.ByteSexpVector(values)
    with pytest.raises(TypeError):
        vec['foo'] = (333, ord(b'z'))

    with pytest.raises(ValueError):
        vec[:2] = (333, ord(b'z'))
Exemple #2
0
 def testByte(self):
     if IS_PYTHON3:
         seq = (b'a', b'b')
     else:
         seq = ('a', 'b')
     sexp = ri.ByteSexpVector(seq)
     is_raw = ri.globalenv.get("is.raw")
     ok = is_raw(sexp)[0]
     self.assertTrue(ok)
Exemple #3
0
 def testInitFromSeqOfBytes(self):
     if IS_PYTHON3:
         seq = (b'a', b'b', b'c')
     else:
         seq = ('a', 'b', 'c')
     v = ri.ByteSexpVector(seq)
     self.assertEqual(3, len(v))
     for x,y in zip(seq, v):
         self.assertEqual(x, y)
Exemple #4
0
    def __value_py_to_r(value, ri):
        """Returns the R equivalent of a python value"""
        val = value.value
        if not isinstance(val, (list, tuple)):
            # Is this an iterable ?
            if hasattr(val,
                       '__iter__') and not isinstance(val, (str, unicode)):
                val = [v for v in val]
            else:
                # In R scalar values are vectors with one element
                # So here we put the scalar value into a list
                val = [val]

        na_value = None
        if value.type == 'bool':
            na_value = ri.NALogicalType()
        elif value.type == 'byte':
            na_value = ri.NAIntegerType(
            )  # I guess that's correct ? That should probably be tested though
        elif value.type == 'float':
            na_value = ri.NARealType()
        elif value.type == 'int':
            na_value = ri.NAIntegerType()
        elif value.type == 'string':
            na_value = ri.NACharacterType()

        # Scan the elements to replace Nones by NA
        val = [(na_value if v is None else v) for v in val]

        if value.type == 'bool':
            return ri.BoolSexpVector(val)
        if value.type == 'byte':
            return ri.ByteSexpVector(val)
        if value.type == 'float':
            return ri.FloatSexpVector(val)
        if value.type == 'int':
            return ri.IntSexpVector(val)
        if value.type == 'string':
            return ri.StrSexpVector(val)

        raise PredictionValueTypeException(value.type)
def test_setitem_slice():
    values = (b'a', b'b', b'c')
    vec = ri.ByteSexpVector(values)
    vec[:2] = b'yz'
    assert tuple(vec) == tuple(b'yzc')
def test_init_from_bytes_in_seq():
    seq = (b'a', b'b', b'c')
    v = ri.ByteSexpVector(seq)
    assert len(v) == 3
    for x, y in zip(seq, v):
        assert ord(x) == y
def test_setitem_int_invalid():
    vec = ri.ByteSexpVector((b'a', b'b', b'c'))
    with pytest.raises(ValueError):
        vec[1] = 333
def test_setitem_int():
    vec = ri.ByteSexpVector((b'a', b'b', b'c'))
    vec[1] = ord(b'z')
    assert vec[1] == ord(b'z')
def test_getitem_slice():
    vec = ri.ByteSexpVector((b'a', b'b', b'c'))
    assert tuple(vec[:2]) == (ord(b'a'), ord(b'b'))
def test_getitem():
    vec = ri.ByteSexpVector((b'a', b'b', b'c'))
    assert vec[1] == ord(b'b')
def test_init_from_seq_invalid_byte():
    seq = (b'a', [], b'c')
    with pytest.raises(ValueError):
        ri.ByteSexpVector(seq)
def test_init_from_bytes():
    seq = b'abc'
    v = ri.ByteSexpVector(seq)
    assert len(v) == 3
    for x, y in zip(seq, v):
        assert x == y
Exemple #13
0
def test_byte():
    seq = (b'a', b'b')
    sexp = ri.ByteSexpVector(seq)
    is_raw = ri.globalenv.find("is.raw")
    assert is_raw(sexp)[0]
Exemple #14
0
 def testInitFromBytes(self):
     seq = (b'a', b'b', b'c')
     v = ri.ByteSexpVector(seq)
     self.assertEqual(3, len(v))
     for x,y in zip(seq, v):
         self.assertEqual(x, y)