Revision: 654
          http://rpy.svn.sourceforge.net/rpy/?rev=654&view=rev
Author:   lgautier
Date:     2008-10-11 15:22:21 +0000 (Sat, 11 Oct 2008)

Log Message:
-----------
Option to find R object as attributes of the r instance.

Modified Paths:
--------------
    branches/rpy_nextgen/NEWS
    branches/rpy_nextgen/doc/source/robjects.rst
    branches/rpy_nextgen/rpy/robjects/__init__.py
    branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py

Modified: branches/rpy_nextgen/NEWS
===================================================================
--- branches/rpy_nextgen/NEWS   2008-09-11 07:08:55 UTC (rev 653)
+++ branches/rpy_nextgen/NEWS   2008-10-11 15:22:21 UTC (rev 654)
@@ -14,6 +14,8 @@
 
 - added functions :func:`get_initoptions` and :func:`set_initoptions`.
 
+- new attribute :attr:`_dotter` for :class:`R` singleton. Setting it to True 
will translate '_' into '.' if the attribute is not found 
+
 Changes
 -------
 

Modified: branches/rpy_nextgen/doc/source/robjects.rst
===================================================================
--- branches/rpy_nextgen/doc/source/robjects.rst        2008-09-11 07:08:55 UTC 
(rev 653)
+++ branches/rpy_nextgen/doc/source/robjects.rst        2008-10-11 15:22:21 UTC 
(rev 654)
@@ -44,7 +44,9 @@
 >>> print(robjects.r)
 
 The instance can be seen as the entry point to an
-embedded R process, and the elements that would be accessible
+embedded R process.
+
+The elements that would be accessible
 from an equivalent R environment are accessible as attributes
 of the instance.
 Readers familiar with the :mod:`ctypes` module for Python will note
@@ -61,7 +63,23 @@
 >>> plot = robjects.r.plot
 >>> dir = robjects.r.dir
 
+This approach has limitation as:
 
+  * The actual Python attributes for the object masks the R elements 
+
+  * '.' (dot) is syntactically valid name for R objects, but not for
+    python objects.
+
+That last limitation can partly be removed by setting the attribute
+:attr:`_dotter` from False to True.
+
+>>> robjects.r.as_null
+# AttributeError raised
+>>> robjects.r._dotter = True
+>>> robjects.r.as_null
+# R function as.null() returned
+
+
 Strings as R code
 -----------------
 

Modified: branches/rpy_nextgen/rpy/robjects/__init__.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/__init__.py       2008-09-11 07:08:55 UTC 
(rev 653)
+++ branches/rpy_nextgen/rpy/robjects/__init__.py       2008-10-11 15:22:21 UTC 
(rev 654)
@@ -440,6 +440,7 @@
     
 class R(object):
     _instance = None
+    _dotter = False
 
     def __init__(self):
         if R._instance is None:
@@ -450,10 +451,28 @@
             #raise(RuntimeError("Only one instance of R can be created"))
         
     def __getattribute__(self, attr):
-        return self[attr]
+        try:
+            return super(R, self).__getattribute__(attr)
+        except AttributeError, ae:
+            orig_ae = ae
 
+        try:
+            return self[attr]
+        except LookupError, le:
+            pass
+
+        if self._dotter:
+            attr = attr.replace('_', '.')
+            try:
+                return self[attr]
+            except LookupError, le:
+                pass
+
+        raise orig_ae
+
     def __getitem__(self, item):
         res = rinterface.globalEnv.get(item)
+            
        res = ri2py(res)
         return res
 

Modified: branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py
===================================================================
--- branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py     2008-09-11 
07:08:55 UTC (rev 653)
+++ branches/rpy_nextgen/rpy/robjects/tests/testRobjects.py     2008-10-11 
15:22:21 UTC (rev 654)
@@ -5,6 +5,11 @@
 
 class RInstanceTestCase(unittest.TestCase):
 
+
+    def tearDow(self):
+        robjects.r._dotter = False
+
+
     def testGetItem(self):
         letters_R = robjects.r["letters"]
         self.assertTrue(isinstance(letters_R, robjects.RVector))
@@ -23,6 +28,12 @@
             self.assertEquals(i, myList[i][0])
 
 
+    def testGetAttr(self):
+        robjects.r._dotter = True
+        self.assertRaises(LookupError, robjects.r.__getitem__, 'as_null')
+        res = robjects.r.as_null
+        self.assertTrue(isinstance(res, rinterface.SexpClosure))
+
     def testEval(self):
         # vector long enough to span across more than one line
         x = robjects.baseNameSpaceEnv['seq'](1, 50, 2)


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 Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to