Revision: 508
          http://rpy.svn.sourceforge.net/rpy/?rev=508&view=rev
Author:   lgautier
Date:     2008-04-29 02:03:05 -0700 (Tue, 29 Apr 2008)

Log Message:
-----------
robjects:
- classes for arrays, matrixes (and almost data.frame)
- more tests

rinterface:
- more tests

Modified Paths:
--------------
    branches/rpy_nextgen/rpy/rinterface/tests/__init__.py
    branches/rpy_nextgen/rpy/robjects/__init__.py
    branches/rpy_nextgen/rpy/robjects/tests/__init__.py
    branches/rpy_nextgen/rpy/robjects/tests/testRObject.py

Added Paths:
-----------
    branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py

Modified: branches/rpy_nextgen/rpy/rinterface/tests/__init__.py
===================================================================
--- branches/rpy_nextgen/rpy/rinterface/tests/__init__.py       2008-04-28 
19:58:14 UTC (rev 507)
+++ branches/rpy_nextgen/rpy/rinterface/tests/__init__.py       2008-04-29 
09:03:05 UTC (rev 508)
@@ -3,6 +3,7 @@
 import test_SexpVector
 import test_SexpEnvironment
 import test_Sexp
+import test_SexpClosure
 import test_SexpVectorNumeric
 
 
@@ -10,10 +11,12 @@
     suite_SexpVector = test_SexpVector.suite()
     suite_SexpEnvironment = test_SexpEnvironment.suite()
     suite_Sexp = test_Sexp.suite()
+    suite_SexpClosure = test_SexpClosure.suite()
     suite_SexpVectorNumeric = test_SexpVectorNumeric.suite()
     alltests = unittest.TestSuite([suite_SexpVector, 
                                    suite_SexpEnvironment, 
                                    suite_Sexp, 
+                                   suite_SexpClosure,
                                    suite_SexpVectorNumeric])
     return alltests
 

Added: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py
===================================================================
--- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py               
                (rev 0)
+++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py       
2008-04-29 09:03:05 UTC (rev 508)
@@ -0,0 +1,37 @@
+import unittest
+import rpy2.rinterface as rinterface
+
+try:
+    #FIXME: can starting and stopping an embedded R be done several times ?
+    rinterface.initEmbeddedR("foo", "--vanilla", "--no-save", "--quiet")
+except:
+    pass
+
+
+class SexpClosureTestCase(unittest.TestCase):
+    #def setUpt(self):
+    #    rinterface.initEmbeddedR("foo", "--no-save")
+
+    #def tearDown(self):
+    #    rinterface.endEmbeddedR(1);
+
+    def testNew(self):
+
+        x = "a"
+        self.assertRaises(ValueError, rinterface.SexpClosure, x)
+        
+    def testTypeof(self):
+        sexp = rinterface.globalEnv.get("plot")
+        self.assertEquals(sexp.typeof(), rinterface.CLOSXP)
+
+    def testRError(self):
+        sum = rinterface.baseNameSpaceEnv["sum"]
+        letters = rinterface.baseNameSpaceEnv["letters"]
+        self.assertRaises(RuntimeError, sum, letters)
+
+def suite():
+    suite = unittest.TestLoader().loadTestsFromTestCase(SexpClosureTestCase)
+    return suite
+
+if __name__ == '__main__':
+     unittest.main()


Property changes on: 
branches/rpy_nextgen/rpy/rinterface/tests/test_SexpClosure.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: branches/rpy_nextgen/rpy/robjects/__init__.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/__init__.py       2008-04-28 19:58:14 UTC 
(rev 507)
+++ branches/rpy_nextgen/rpy/robjects/__init__.py       2008-04-29 09:03:05 UTC 
(rev 508)
@@ -106,6 +106,9 @@
     def do_slot(self, name):
         return self._sexp.do_slot(name)
 
+    def rclass(self):
+        return baseNameSpaceEnv["class"](self.getSexp())
+
 class Rvector(Robject):
     """ R vector-like object. Items in those instances can
        be accessed with the method "__getitem__" ("[" operator),
@@ -176,8 +179,43 @@
         return res
 
     def __len__(self):
-        return len(self._sexp)
+        return len(self.getSexp())
 
+class RArray(Rvector):
+
+    def __init__(self, o):
+        super(RArray, self).__init__(o)
+        if not r["is.array"](self.getSexp())[0]:
+            raise(TypeError("The object must be reflecting an R array"))
+
+    def __getattr__(self, name):
+        if name == 'dim':
+            res = r.dim(self.getSexp())
+            res = mapperR2Py(res)
+            return res
+
+    def __setattr__(self, name, value):
+        if name == 'dim':
+            value = mapperPy2R
+            res = r["dim<-"](value)
+        
+class RMatrix(RArray):
+    
+    def nrow(self):
+        """ Number of rows """
+        return r.nrow(self.getSexp())
+
+    def ncol(self):
+        """ Number of columns """
+        return r.nrow(self.getSexp())
+
+class DataFrame(Rvector):
+    #FIXME: not implemented
+    def __init__(self, o):
+        raise(RuntimeError("Not implemented."))
+
+
+
 class Rfunction(Robject):
     """ An R function (aka "closure").
     
@@ -218,6 +256,11 @@
     def __iter__(self):
         return iter(self._sexp)
 
+    def get(self, item):
+        res = self.getSexp().get(item)
+        res = mapperR2Py(res)
+        return res
+
 class RS4(Robject):
     def __init__(self, o):
         if (isinstance(o, rinterface.SexpS4)):

Modified: branches/rpy_nextgen/rpy/robjects/tests/__init__.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/tests/__init__.py 2008-04-28 19:58:14 UTC 
(rev 507)
+++ branches/rpy_nextgen/rpy/robjects/tests/__init__.py 2008-04-29 09:03:05 UTC 
(rev 508)
@@ -2,6 +2,7 @@
 
 import testRObject
 import testRVector
+import testRArray
 import testRFunction
 import testREnvironment
 import testRobjects
@@ -9,11 +10,13 @@
 def suite():
     suite_RObject = testRObject.suite()
     suite_RVector = testRVector.suite()
+    suite_RArray = testRArray.suite()
     suite_RFunction = testRFunction.suite()
     suite_REnvironment = testREnvironment.suite()
     suite_Robjects = testRobjects.suite()
     alltests = unittest.TestSuite([suite_RObject,
-                                   suite_RVector,
+                                   suite_RVector,                   
+                                   suite_RArray,
                                    suite_RFunction,
                                    suite_REnvironment,
                                    suite_Robjects ])

Modified: branches/rpy_nextgen/rpy/robjects/tests/testRObject.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/tests/testRObject.py      2008-04-28 
19:58:14 UTC (rev 507)
+++ branches/rpy_nextgen/rpy/robjects/tests/testRObject.py      2008-04-29 
09:03:05 UTC (rev 508)
@@ -23,6 +23,19 @@
         prt = rinterface.baseNameSpaceEnv["print"]
         s = prt.__repr__()
 
+    def testRclass(self):
+        self.assertEquals("character",
+                          robjects.baseNameSpaceEnv["letters"].rclass()[0])
+        self.assertEquals("numeric",
+                          robjects.baseNameSpaceEnv["pi"].rclass()[0])
+        self.assertEquals("function",
+                          robjects.globalEnv.get("help").rclass()[0])
+
+    def testDo_slot(self):
+        self.assertEquals("A1.4, p. 270",
+                          
robjects.globalEnv.get("BOD").do_slot("reference")[0])
+
+
 def suite():
     suite = unittest.TestLoader().loadTestsFromTestCase(RObjectTestCase)
     return suite


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to